Do you want to hide any category from the shop page in your WooCommerce store? By default, WooCommerce shows products from all categories on the shop page. You may want to prevent products from a certain category from being shown all together.
WooCommerce does not have a built-in solution for this. This means that we have to use plugins or custom PHP code.
However, we recommend using custom code snippets, as this is the safest way to make changes. You should also ensure that you use a child theme so that your changes are not lost during an update.
Hide Category From Shop Page WooCommerce
In this brief tutorial, we will demonstrate how you can hide a category from the shop page in your WooCommerce store.
We will also share how you can hide a category from users who belong to a user role.
Let us have a look at how you can do this.
Steps to Hide Products From a Category on the WooCommerce Shop Page
Here are the steps that you need to follow:
- Log into your WordPress site and access the Dashboard as the admin user.
- From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file where we will add the function to hide products from a category on the WooCommerce shop page.
- Add the following code to the php file:
add_filter( ‘woocommerce_product_query_tax_query’, ‘njengah_hide_shop_categories’);
function njengah_hide_shop_categories($tquery) {
$hidden_categories = array("tyres", "car parts");
if (is_shop()) {
$tquery[] =
array(
‘taxonomy’ => ‘product_cat’,
‘terms’ => $hidden_categories,
‘field’ => ‘slug’,
‘operator’ => ‘NOT IN’
);
}
return $tquery;
}
[/php]- Remember to add the correct name of the category to hide its products.
How the code works
We have used the ‘woocommerce_product_query_tax_query’ filter, which allows us to add more taxonomy search criteria to the current query.
First, we check if we are on the shop page using the “is_shop()” function.
After that, we added an array to the taxonomy query that will exclude certain categories based on their slugs. We have excluded ‘tires’ and ‘car parts’ categories.
We have tested this code and it works very well.
However, what if you want to hide products from a certain category based on the user role?
Hiding A Category from Users who Belong to a user Role
You might want to hide a category from a certain user role. For example, you might have a category of discounted products that you want to offer to your customers.
Here are the steps that you need to follow:
- Log into your WordPress site and access the Dashboard as the admin user.
- From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file where we will add the function to hide a category from users who belong to a user role.
- Add the following code to the php file:
add_filter( 'woocommerce_product_query_tax_query', 'njengah_hide_shop_categories_by_role'); function njengah_hide_shop_categories_by_role($tquery) { $user = wp_get_current_user(); $blocked_user_roles = array("customer","administrator"); $hidden_categories = array("tyres", "car parts"); if (is_shop() && (!is_user_logged_in() || is_user_logged_in() && count(array_intersect($blocked_user_roles,$user->roles)) > 0)) { $tquery[] = array( 'taxonomy' => 'product_cat', 'terms' => $hidden_categories, 'field' => 'slug', 'operator' => 'NOT IN' ); } return $tquery; }
How the Code Works
This snippet works like the first one, but we have added the details of the current user using the wp_get_current_user() function.
After that, we have defined an array of categories that we want the roles to be hidden from.
The if statement checks to see if we’re on the shop page as before, and a logic that will return true if the user is not logged in or if they have been blocked.
This ensures that the categories are hidden from the user role.
Conclusion
By now, you should be able to hide products from a certain category on the shop page. We have also illustrated how you can hide a category from a user role.
The code snippets shared above have been tested and they work well.
If you encounter any problems, please consider consulting a qualified WordPress developer.
Similar Articles
- 100+ Tips, Tricks & Snippets Ultimate WooCommerce Hide Guide
- How to Remove, Rename and Add Storefront Sorting Options
- How to Reorder Categories Shop Page WooCommerce Storefront Theme
- How to Get Product Description & Display WooCommerce
- How to Hide a Product in WooCommerce or Hide Products by Category or Roles
- How to Change Add to Cart Button to Read More WooCommerce
- How to Add Product Image WooCommerce Checkout Page
- How to Hide Trailing Zeros WooCommerce
- How to Set Up WooCommerce Per Product Shipping
- How to Hide Product SKU In WooCommerce
- How to Hide Dashboard on the My Account Page WooCommerce
- How to Hide & Rename Addresses Tab My Account Page WooCommerce
- How to Hide WooCommerce Product Count
- How to Hide Price If Zero In WooCommerce
- How to Add WooCommerce Storefront Product Pagination
- How to Hide Order Status In WooCommerce
- How to Hide Product Gallery In WooCommerce
- How to Disable Payment Method for Specific Category
- How to Add WooCommerce Custom Checkout Message
- How to Create WordPress Custom User Roles