0

I've been working on trying to hide all other categories from the category widget when a particular category is selected.

So, as an example, the site has 5x Products categories and and 1x Services category. When browsing the Services I do not want anything from the 5x products categories to display in the categories widget. The below code works fine to hide the 5x product categories but it hides it site wide, which I don't want.

add_filter( 'woocommerce_product_categories_widget_args', 'exclude_widget_category' );
function exclude_widget_category( $args ) {
    $exclude_terms = array();
    array_push( $exclude_terms, 2548, 2245, 2775, 2913, 2846 );
    $termchildren = get_term_children( '2548, 2245, 2775, 2913, 2846', 'product_cat' );
    foreach( $termchildren as $child ) {
        $term = get_term_by( 'id', $child, 'product_cat' );     
        array_push( $exclude_terms, $term->term_id );
    }
    $args['exclude'] = $exclude_terms;

    return $args;
}

So to target the Services cat I added is_product_category and this works OK when on the services category but if i'm in a products category it completely breaks the page (no products appear, no categories etc). It also results in all subcategories in Services behaving like that too.

if ( is_product_category(2921) ) {

(2921 is my services category and 2548, 2245, 2775, 2913, 2846 are my product categories)

Also, I may as well add this now; need this to work the other way as well, so if you are in any of the products categories then the services categories should be hidden.

What am I doing wrong?

Many thanks.

5
  • Your code example for when using is_product_category is incomplete so it's unclear what is inside the if statement, but I suspect you are unaware that filters always return something, and your filter only returns in the scenario you wanted to make a modification. When it is not that scenario you're supposed to return it unchanged/unmodified. If you do not return a value, it doesn't just use the original, it uses an empty undefined value, aka nothing
    – Tom J Nowell
    Jul 5, 2021 at 18:00
  • Also, I'm unfamiliar with the woocommerce_product_categories_widget_args filter, is that a WooCommerce specific filter? 3rd party plugin dev support is off topic here, if you need help with WooCommerce you should ask via their support routes, e.g. slack/forums/FB group/etc
    – Tom J Nowell
    Jul 5, 2021 at 18:01
  • Ah I see. I thought questions like this were outside of the scope of what woocommerce are willing to help with. Thanks for responding anyway.
    – Forau
    Jul 5, 2021 at 18:23
  • They may not be willing to help you via the official routes, and if so that is unfortunate, but 3rd party plugin development support is not in this stacks scope. Other WooCommerce users will be able to direct you to an appropriate place
    – Tom J Nowell
    Jul 5, 2021 at 21:02
  • Went about it a different way :) I got no help from woocommerce or the community but got there in the end. Posted answer below in case anyone else wants same functionality.
    – Forau
    Jul 25, 2021 at 17:27

1 Answer 1

0

Went about it a different way, works OK.

Just in case anyone else is looking for similar functionality;

//* dropdown
add_filter( 'woocommerce_product_categories_widget_dropdown_args', 'woo_product_cat_widget_args' );
//* list
add_filter( 'woocommerce_product_categories_widget_args', 'woo_product_cat_widget_args' );
function woo_product_cat_widget_args( $cat_args ) {
    if ( is_product_category( 'experiences' ) || term_is_ancestor_of( 2921, get_queried_object_id(), 'product_cat' ) ) { 
   $cat_args['exclude'] = array(2548, 2245, 2775, 2913, 2846);
                return $cat_args;
} 
    else { 
    $cat_args['exclude'] = array('2921');
                return $cat_args;
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.