• Resolved forau

    (@forau)


    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 im 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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Hi there ??

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers.

    Thread Starter forau

    (@forau)

    Thanks Gabriel.

    Just to expand on my previous post, i’ve tweaked the code and come up with the below;

    add_filter( 'woocommerce_product_categories_widget_args', 'exclude_widget_category' );
    function exclude_widget_category( $args ) {
    	if ( is_product_category(2921) ) {	
        $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;
    }
    	else {
    	$exclude_terms = array();
        array_push( $exclude_terms, '2921' );
        $termchildren = get_term_children( '2921', '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;
    }
    }

    When I go to my Services category it displays fine, just Services with its subcats. But if I navigate to one of those subcats the products menu (with 5x cats) appears. Im really not sure what i’m doing wrong!

    Plugin Support lionel.a11n

    (@lioneldaniel)

    Hello @forau,

    We haven’t heard any further responses in a while, so I’m going to mark this as resolved – we’ll be here if and/or when you are ready to continue, or you can start a new topic with another question.

    I recommend visiting the resources gabrielfuentes shared, as you may find other developers hanging out in Slack or the Facebook group interested in helping you debug custom code.

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add filter for specific category only’ is closed to new replies.