How to Hide a Category from the Shop Page in Woocommerce

By default, the shop page in Woocommerce will show products from all categories, but if you want to prevent a category from being shown altogether, or from specific customers what steps can you take?

Hiding A Category With a Snippet

Here’s a snippet that will hide specific categories from the shop page.

How this works: Using the ‘woocommerce_product_query_tax_query’ filter, which allows us to add more taxonomy search criteria to the current query,  we firstly check to see if we are on the shop page using the “is_shop()” function.

We then add an array to the taxonomy query that will exclude certain categories based on their slugs, in the example above we exclude the “tshirts” and “hoodies” categories.

Hiding A Category from Users who Belong to a user Role

Whilst the snippet above will hide categories, you might want a little more control as to who the categories are hidden from. For example, you might have a category of discounted products that you only want to show to preferred customers. The snippet below will hide categories from a specific user role.

How this works: The basis of this snippet is the same as the one above it, but we now get the details of the current user using the wp_get_current_user() function. We then define an array of roles that we want the roles to be hidden from. In this example we only want the “tshirts” and “hoodies” categories to be seen by a select band of users.

We then use the if statement below before running our logic to hide the category

The first part of the if statement checks to see if we’re on the shop page as before, then we run some or logic that will return true if the user is not logged in or they are a member of one of the blocked user roles. This should ensure that our categories show to logged in users who are not members of the blocked categories.

Hiding A Category from a Specific Users

We can change our user query slightly to block users by username rather user role

How this works: This is very similar to the “roles” snippet, but instead of using an array of blocked user roles we create an array of blocked users.

We then set-up our if logic so the categories are only shown to logged in users who do not have a username contained in the blocked username list.

Where should these snippets be placed?

The snippets should be added to your themes ‘functions.php’ file or via a plug-in such as Code Snippets. Note that the snippets are designed to be used in isolation, you may get odd results if you add multiple snippets.

But, I Don’t Want to Use Snippets/These Snippets Don’t Do Exactly What I Need

If you’re looking for a plug-in that will hide categories I would recommend the “WooCommerce Hide Categories Plugin” from Barn2Media, the plugin allows you to hide categories via an easy to use admin interface and also provides the following functionality.

Categories can be completely hidden – The snippets above only hide categories from the shop page and although the functionality could be changed by adding additional conditional logic, it would be much harder to hide a category from all the pages on your site.

You might, for instance, have categories that you want hidden appearing in your navigation menus, the Hide Category plugin will make sure any menu items linked to hidden categories are also hidden.

Products linked to hidden categories are also hidden – Whilst the snippets above hide categories, if a user entered a URL to a product that was contained in a hidden category they would still be shown the product’s details page and from there they could add the “hidden” product to their basket. The hide category plugin makes all products linked to hidden categories hidden as well, so users have no way of accessing them. If a user did enter a URL to a hidden product then the plugin would redirect them to your store’s 404 page.

Password protected categories – The snippets above hide categories by user role and user id, and the hide categories plugin provides both of these methods to hide categories as well. It also allows you to set passwords against categories so users can enter a password and then be allowed access to a category and all its products. The plugin handles the display of the login forms and the logic to log the users in.

You can can find out more details about the “WooCommerce Hide Categories Plugin” here.

Conclusion

Hiding categories can help you organize your Woocommerce store and allow you to sell chosen products to specific users or user groups. Hopefully, the snippets provided above and the recommended plug-in will allow you to set up your store categories exactly how you want them. If you have any questions or comments about hiding categories please don’t hesitate to leave them below.

Leave a Comment