In Woocommerce,

In Woocommerce, I have several categories with subcategories. When in the category Loyalty or its child categories page, I just want to show the Loyalty and its child categories in the Categories Widget (Sidebar).

In the Shop page, Categories Widget should show all categories except Loyalty and its child categories.

Any help or guidance?

  • Konstantinos Xenos

    Hi Munro !

    Could you add the following code into your child theme functions.php and give it a try ?

    The only thing you have to change is to insert your Loyalty category slug replacing the INSERT_SLUG_HERE in the code. In example if the slug is 'loyalty' then the line should become $category_id = get_term_by( 'slug', 'loyalty', 'product_cat' );

    add_filter( 'woocommerce_product_categories_widget_args', 'munro_check_loyalty_cat' );
    function munro_check_loyalty_cat( $cat_args ) {
    	$category_id  = get_term_by( 'slug', 'INSERT_SLUG_HERE', 'product_cat' );
    	$children_ids = get_terms( 'product_cat', array( 'child_of' => $category_id->term_id ) );
    	$cats         = array( $category_id->term_id );
    	if ( ! empty( $children_ids ) ) {
    	 	foreach ( $children_ids as $children ) {
    	 	 	$cats[] = $children->term_id;
    	if ( ! in_array( $cat_args['current_category'], $cats ) ) {
    	 	$cat_args['exclude'] = $cats;
    	} else {
    	 	$cat_args['include'] = $cats;
    	return $cat_args;


Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.