• njkuiper6

    (@njkuiper6)


    Hi

    I was running this function to hide subcategory products from parent categories.

    function exclude_product_cat_children($wp_query) {
    if ( isset ( $wp_query->query_vars['product_cat'] ) && $wp_query->is_main_query()) {
        $wp_query->set('tax_query', array(
                                        array (
                                            'taxonomy' => 'product_cat',
                                            'field' => 'slug',
                                            'terms' => $wp_query->query_vars['product_cat'],
                                            'include_children' => false
                                        )
                                     )
        );
      }
    }
    add_filter('pre_get_posts', 'exclude_product_cat_children');

    But since the last update is has not been working.
    anyone have an idea why it isn’t?

    Thanks!

    https://www.ads-software.com/plugins/woocommerce/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    Looks fine; are you sure your code is still present, and hasn’t been wiped by an update to the theme/plugin?

    Thread Starter njkuiper6

    (@njkuiper6)

    Yes it is present, I will have to start really digging into everything running on the site.
    Just didn’t know if something was tweaked in the lastest update that would make the code fail.

    Would it be possible for the core to have this function already in it with a check-box to enable it? (just a thought that flew through my head)

    Thanks for the help!

    Plugin Contributor Mike Jolley

    (@mikejolley)

    Maybe do a var_dump to see if $wp_query->query_vars[‘product_cat’] is actually set. That should be what you start with during debug.

    Hello,

    I use this code (works great!) however with the most recent version of WooCommerce it no longer works with the Layered Nav widget – the widget no longer filters when this code is present. Can anyone help me?

    Yes SS88_UK, a fix for this would be great, any news on this?

    Hey.

    It’s been a while I’m fairly confident this works (I can’t remember exactly!!)

    function woo_hide_product_categories_widget( $list_args ) {
     
                $list_args[ 'hide_empty' ] = 1;
     
                return $list_args;
     
    }
     
    add_filter( 'woocommerce_product_categories_widget_args', 'woo_hide_product_categories_widget' );

    Or it could be this – sorry totally can’t remember!!

    function kia_no_child_terms($wp_query) {  
    	if ( isset ( $wp_query->query_vars['product_cat'] ) && !is_admin() && $wp_query->is_main_query())
      		$wp_query->tax_query->queries[0]['include_children'] = 0;
    }
    add_action('parse_tax_query', 'kia_no_child_terms');

    It was the second one, thanks a lot SS.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘hiding subcategory products from parent category’ is closed to new replies.