How To Hide Product Categories on WooCommerce Shop Page

When working with WooCommerce, I often come across clients who need to hide specific products from the general shop page.

Whether you’re online store features private categories or you’re running catalogue mode (no payment integration), WooCommerce has the ability to hide specific product categories from the product archive also known as the shop page.

For this example, let’s imagine we have a website selling car parts. Since we want to focus on selling new parts, we’re going to hide all products under the category “Used” from the general shop page. Instead we only want users to view “Used” cart parts when clicking on the “Used” category from our navigation menu.

To hide a specific category from the WooCommerce shop page, add the following code to your theme’s functions.php file.

add_action( 'woocommerce_product_query', 'prefix_custom_pre_get_posts_query' );
/**
 * Snippet: Hide Product Cateories from shop page in WooCommerce
 *
 */
function prefix_custom_pre_get_posts_query( $q ) {
	
	if( is_shop() ) { // set conditions here
	    $tax_query = (array) $q->get( 'tax_query' );
	
	    $tax_query[] = array(
	           'taxonomy' => 'product_cat',
	           'field'    => 'slug',
	           'terms'    => array( 'used' ), // set product categories here
	           'operator' => 'NOT IN'
	    );
	
	
	    $q->set( 'tax_query', $tax_query );
	}
}

Posted on July 15, 2019 | Categories: Blog