Hide Products In A Category From Shop

hide products in a category from shop

Hide Products In A Category From Shop

 |   |  0
WooCommerce WordPress

Here, I will share the code for Hide products in a category from shop in WordPress – WooCommerce.

Filter / actions I used:-

add_filter( 'woocommerce_product_is_visible', 'hide_enrolment_products_from_shop', 10, 2 );
add_action( 'woocommerce_product_query', 'hide_enrolment_cat_from_shop' );

Callback:-

function hide_enrolment_products_from_shop( $is_visible, $id ) {
	$category = getProductCategory('', $id);
	if($category == 'Enrollment') {
	  $is_visible = false;
	}
	return $is_visible;
}
 
// Exclude products from a particular category on the shop page
function hide_enrolment_cat_from_shop( $q ) {
	$tax_query = (array) $q->get( 'tax_query' );
	 
	// Don't display products in the Enrollment category on the shop page.
	$tax_query[] = array(
	  'taxonomy' => 'product_cat',
	  'field' => 'slug',
	  'terms' => array( 'Enrollment' ),
	  'operator' => 'NOT IN'
	);
	$q->set( 'tax_query', $tax_query );
}

If you got error getProductCategory function undefined, add it from Here.

Enjoy…


0 Claps

Show your love in the form of Claps and Comments...

Comments...

No comments found. Leave your reply here.