Introduction
When you install WooCommerce all necessary pages for this plugin to work properly on WordPress are created. These are pages like Shop, Cart, My account.
The shop page is where all WooCommerce products from all product categories are listed. Actually the shop page is defined by going to WooCommerce > Settings > Products > General > Shop page.
Sometimes it is needed to hide some product categories from this main shop page where all products are listed. This means that products from the rest of categories are visible on the shop page and products under certain categories will be removed from this page.
In our example I have created 4 products under 4 different categories (furniture, electronics, books, food).
How to hide or remove products from a certain category on the shop page
To exclude WC products under a certain category on the shop page we will use a PHP code which will be added to functions.php
file which it is found in your theme or child theme folder.
We will use woocommerce_product_query
WooCommerce hook and using a custom function we will alter the products categories query results:
/**
* Exclude products from a single category or multiple categories on the shop page
*/
function zi_exclude_prod_categ_shop( $query ) {
$zi_query = (array) $query->get( 'tax_query' );
$zi_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'furniture'), // Don't display products in the furniture category on the shop page
'operator' => 'NOT IN'
);
$query->set( 'tax_query', $zi_query );
}
add_action( 'woocommerce_product_query', 'zi_exclude_prod_categ_shop' );
The above code, after it is added to functions.php and then functions.php it is uploaded on FTP in theme or child theme folder, will exclude from the shop page all products from "furniture" category ('terms' => array( 'furniture')
). As you can see the product category slug is used. The product category slug can be found by going to Products > Categories > Edit category:
After the code it's added, the Shop page will look like this:
How to hide or remove products from multiple categories on the shop page
What if you want to exclude WooCommerce products from multiple categories on the shop page? We will use a similar code added to the same file functions.php:
/**
* Exclude products from a single category or multiple categories on the shop page
*/
function zi_exclude_prod_categ_shop( $query ) {
$zi_query = (array) $query->get( 'tax_query' );
$zi_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'furniture','books','food'), // Don't display products in the furniture, books and food categories on the shop page
'operator' => 'NOT IN'
);
$query->set( 'tax_query', $zi_query );
}
add_action( 'woocommerce_product_query', 'zi_exclude_prod_categ_shop' );
After the above code it's added the Shop page will shwo this:
Conclusion
With just a small piece of PHP code we can hide WC products from a category or on more categories really easy. This way you can have a customized Shop page where you can sell products only from certain categories while you can have separate pages for other categories.
Comments closed
Please contact me, if you have any questions or suggestions.