Hide WooCommerce Product Categories on Shop Page or other Page


Hide WooCommerce Product Categories on Shop Page or other Page web

You can hide certain WooCommerce product categories from the shop page or other targetted pages, by using an action called wooCommerce_product_query

Add it in your theme via functions.php and it works well…

Or you can easily add this code through plugin called “Code Snippets”.

First of all, you need to activate the plugin called “Code Snippets”.
Then go to the snippets page on the dashboard and click on “add new”. Enter the title you want to call.

Now, Simply add this snippet code mention in below and click “Save changes and activate”.

//<~ dont add me into functions.php 

add_action( 'woocommerce_product_query', 'prefix_custom_pre_get_posts_query' );
/**
 * Hide Product Cateories from targetted pages in WooCommerce
 * @link https://gist.github.com/stuartduff/bd149e81d80291a16d4d3968e68eb9f8#file-wc-exclude-product-category-from-shop-page-php
 *
 */
function prefix_custom_pre_get_posts_query( $q ) {
	
	if( is_shop() || is_page('awards') ) { // set conditions here

	    $tax_query = (array) $q->get( 'tax_query' );
	
	    $tax_query[] = array(
	           'taxonomy' => 'product_cat',
	           'field'    => 'slug',
	           'terms'    => array( 'cat1', 'cat2' ), // set product categories here
	           'operator' => 'NOT IN'
	    );
	
	
	    $q->set( 'tax_query', $tax_query );
	}
}

So the action is wrapped in a conditional statement that targets the pages you want the Product Categories to not display.

Related: How To Display Coupons Used In An Order by email In woocommerce

Related: How To Remove Products On Checkout Page in WooCommerce

 

Well, I hope this article must have helped you. If you have any problem regarding this post just drop your comment here or you can also say “Thanks” simply.

Have fun! 🙂