WooCommerce products – restrict view by category

How to limit a list of WooCommerce product available to the site visitors by a WooCommerce category. Example: You need Customers to visit the shop and see all categories and products except the category XYZ. Only users assigned role ABC can see category XYZ or its products.
User Role Editor Pro content view restrictions add-on will help:

Content view access restriction for selected roles

This video shows how to setup the required permissions for our example:
Products 4, 5, 6 belong to the category XYZ and can be viewed at front-end by the users with role ABC only:

In case you have a lot of products but don’t have enough time to update all of them manually this code may help you

Set your own parameters, like category and role, instead of 55 (‘XYZ’) – line #11, and ‘abc’ – line #15, place .php file with updated code as a Must Use plugin to the wp-content/mu-plugins/ folder and open your site admin backend 1 time. All products of a category XYZ will get a content view restriction automatically.
It’s a one time execution code. While it has protection and will not update posts twice, it’s better to remove this file after checking that posts were updated correctly.