You don’t always want to let all your users see all your products. In this article I’m going to show you how you can hide products by user roles in WooCommerce.
Hide products by user roles in WooCommerce
Let’s imagine that we’ve got certain limited-edition products on our store that we only want to offer to certain customers. One of the easiest ways to ensure that only the right people see these products is to hide them from users who do not have the correct role.
In this article we’ll take a look at some key information, including:
- What are user roles
- How to create new user roles in WooCommerce
- How to automatically assign a user role to a customer
We’ll then walk through:
- How to hide a single WooCommerce product by user roles
- Hiding all products in specific WooCommerce categories by user roles
- Hiding all products in our WooCommerce store by user roles
Finally, we’ll look at some optional behaviours such as:
- Redirecting the user when they try to access a hidden product, e.g. to a dedicated landing page
- Changing the ‘Add to Cart’ button text for our ‘hidden’ products
For all the functionality discussed in this article, you’ll need a copy of WooCommerce Members Only, which is a membership plugin for WooCommerce. This will allow you to hide products by user roles in your WooCommerce store.
By the way, here’s a complete tutorial on how to build a WooCommerce membership site.
And there is a more in-depth article on WooCommerce hidden products here.
What do we mean by ‘hidden’?
Before we start, let’s just clarify what we mean by hidden. A product that is hidden in WooCommerce will not:
- Be accessible directly
- Be present in any archive or shop page (the standard WooCommerce loop)
- Appear in product lists generated using PHP snippets or shortcodes
- Be visible to search engines
What are user roles?
Roles in WordPress are used to define what individual users can see and do on a site. For instance, a user with the role of ‘Administrator’ can do anything they like – view and edit all content, upload and delete themes and plugins, add and remove other users.
We can use the concept of user roles in our WooCommerce store to give certain customers more rights and privileges than other customers. For example, we could create a user role that allows users to see certain products that would be hidden from other customers.
You’ll find more details about WordPress user roles here.
How to create new user roles in WordPress
The Members Only plugin makes it easy to create new user roles in WooCommerce.
Let’s say we want to create a user role called ‘Star Customers’ who will be entitled to purchase our limited edition products. We’ll hide those products from every other customer who does not have the Star Customer user role.
Creating a new role in WooCommerce
To create our new WooCommerce user role:
- Go to WooCommerce > Settings > Members Only > User Roles
- Select the ‘Customer’ user role
- Enter your new role name
- Hit ‘Duplicate role’
That’s all you need to do to create your new WooCommerce user role.
Automatically assigning a new user role
Once you’ve created your new user role, you might want to automatically assign it to certain customers – e.g. if they buy certain products.
To find out how to automatically assign a user role to a customer after purchasing a product in WooCommerce, see this article.
With our new user role set up, it’s time to look at exactly how to hide products by user role in WooCommerce.
How to hide a single WooCommerce product by user roles
Let’s imagine that we just have one product that we want to hide from certain user roles. To hide one product:
- Edit the product that you wish to hide
- Click on the ‘Members Only’ tab in the ‘Product Data’ section
- Select ‘By User Role’ in the ‘Restriction method’ field
- Enter the user roles that are permitted to view this product in the ‘User Roles’ field
- Check the ‘Hide in Archives’ box
- Save the product
This will now ensure that this product will be hidden from any users who do not have the ‘Star Customer’ role, including users who are not logged in.
Multiple user roles
Note that you can enter multiple user roles if you wish. The user will just need to have one of the specified user roles in order to view the product.
To enable multiple user roles in WooCommerce take a look at this article on user approvals.
How to hide multiple WooCommerce products by user roles
The simplest way to hide multiple WooCommerce products by user roles is to hide specific categories. This will mean that all products in the restricted categories will be hidden.
Let’s have a look at how to hide products in WooCommerce categories by user roles.
- Go to WooCommerce > Settings > Members Only
- Select ‘User Role’ in ‘Restriction Method’
- Select ‘Specified Categories’ in ‘Restricted Content’
- Enter the categories you want to hide in ‘Restricted Categories’
- Finally, in ‘Permitted User Roles’ enter one or more user roles which are allowed to view the hidden product categories
With these settings, hidden categories will not appear in any category archive, products within the hidden categories will not appear, and products will not be accessible directly.
Using this method to hide products is also called private or protected categories. There’s a longer article on WooCommerce protected categories here, including how to set different user role restrictions for different categories.
How to hide all products in WooCommerce by user roles
So the final option is for us to hide all WooCommerce products by user roles. We might do this because we want to make our entire product catalogue only available to a selected few.
To hide all WooCommerce products by user roles:
- Go to WooCommerce>Settings>Members Only
- Select ‘User Role’ in ‘Restriction Method’
- Select ‘All WooCommerce Pages’ in ‘Restricted Content’
- In ‘Permitted User Roles’ enter one or more user roles that are allowed to view the hidden products
Using this method, all product, archive and shop pages will be hidden from the user if they don’t have the correct user role.
Redirecting the user to a dedicated landing page
Now let’s look at some extra options. If a customer tries to access a product page that is hidden, you might want to redirect them to a specific page – e.g. a landing page with further information, or even another product page that will allow them to upgrade their account.
To enable this redirection, just set the ‘If Restricted, Redirect To’ field in WooCommerce>Settings>Members Only to your landing page.
Changing the ‘Add to Cart’ button text for our ‘hidden’ products
What if we don’t want to hide the products but just make them inaccessible? We can still show the products on our website to tempt those customers who don’t have the correct user role. But we can change the ‘Add to Cart’ button to display some different text. When the user clicks the product, they’re redirected to our landing page – see above.
To change the button text, just update the ‘Add To Cart Text’ setting. You’ll also need to deselect the ‘Hide Products in Archives’ setting so that the products are no longer hidden.
Check out this article for how to hide the price in WooCommerce
Hiding products in WooCommerce by user roles
This article should have given you all the information you need to hide WooCommerce products according to user roles.
We looked at what user roles are and how to create them using the Members Only plugin.
We also looked at different options for hiding products – either individually, by category, or globally.
Then we looked at some ways to finesse our site by adding redirections.
I hope you found this useful. Don’t forget to pick up your copy of WooCommerce Members Only.