skip to Main Content

I am developing b2b section in my Woocommerce shop. I have managed to filter woocommerce_product_query_meta_query to display only products that have been enabled for b2b section for b2b users.

However I cannot find a way to hide product categories that shows 0 results (because no product enabled for b2b section is in that category) in Woocommerce category widget.

I was thinking about overriding default Woocommerce widget code and for each category (and subcategory) run wp query that returns number of products in this category that are enabled for b2b. But with large number of products and categories it seems very inefficient.

Is there a way to hide categories from Woocommerce category widget that are "empty" (no product in this category is enabled for b2b)?

Thanks for any suggestions.

Edit

To clarify my question: here is the function I use to filter product query to display only products that have _eda_display_in_b2b meta set to yes:

function show_only_b2b_products( $meta_query, $query ) {
    if ( is_admin() || ! is_user_logged_in() || ! is_b2b_user() ) {
        return $meta_query;
    }

    $meta_query[] = array(
        'key'     => '_eda_display_in_b2b',
        'value'   => 'yes',
        'compare' => '='
    );

    return $meta_query;
}

add_filter( 'woocommerce_product_query_meta_query', 'show_only_b2b_products', 10, 2 );

Exaple:
https://klon.vozikyprozivot.cz/kategorie-produktu/pridavne-pohony/

This category is not empty for regular customers and not logged in users. But for b2b customers there is no product to show. So I need to hide this category from the widget for b2b customers.

2

Answers


  1. Chosen as BEST ANSWER

    With a big help from Harshit Vaid I have managed to solve it:

    add_filter( 'woocommerce_product_categories_widget_dropdown_args', 'eda_exclude_wc_widget_categories' );
    add_filter( 'woocommerce_product_categories_widget_args', 'eda_exclude_wc_widget_categories' );
    
    function eda_exclude_wc_widget_categories( $cat_args ) {
    
        $args = array(
            'taxonomy'   => 'product_cat',
            'hide_empty' => 0
        );
    
        $all_categories        = get_categories( $args );
        $category_exclude_list = array();
    
        foreach ( $all_categories as $cat ) {
            if ( $cat->category_parent == 0 ) {
                $category_id = $cat->term_id;
    
                $product_args = array(
                    'posts_per_page' => - 1,
                    'post_type'      => 'product',
                    'tax_query'      => array(
                        array(
                            'taxonomy' => 'product_cat',
                            'terms'    => $category_id,
                            'field'    => 'term_id',
                            'operator' => 'IN'
                        )
                    ),
                    'meta_query'     => array(
                        array(
                            'key'   => '_eda_display_in_b2b',
                            'value' => 'yes'
                        )
                    )
                );
    
                $query = new WP_Query( $product_args );
                $count = $query->post_count;
    
                if ( $count == 0 ) {
                    array_push( $category_exclude_list, $category_id );
                }
            }
        }
    
        $cat_args['exclude'] = $category_exclude_list;
    
        return $cat_args;
    }
    

  2. If you are referring to the product categories widget, there is a setting for hiding empty categories:enter image description here

    If you are referring to something different, could you please share an example page URL as well as your site’s System Status? You can find it via WooCommerce > Status. Select “Get system report” and then “Copy for support”. Once you’ve done that, paste it here in your response.

    Hope this will help you.

    ======Edit======

    I think for the above issue you can use the wc category hook and remove the category. Please check the below code

            //* Used when the widget is displayed as a dropdown
        add_filter( 'woocommerce_product_categories_widget_dropdown_args', 'rv_exclude_wc_widget_categories' );
        //* Used when the widget is displayed as a list
        add_filter( 'woocommerce_product_categories_widget_args', 'rv_exclude_wc_widget_categories' );
        function rv_exclude_wc_widget_categories( $cat_args ) {
        //add the logic to check the category have product or not and make the array of ID and replace the below array with that.
            $cat_args['exclude'] = array('55','68'); // Insert the product category IDs you wish to exclude
            return $cat_args;
        }
    
    

    In the above code I think you can make logic and check if the category have the products or not and make the array of id for the non-product category.

    In this way you can exclude the category from the list and dropdown.

    How this will help you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search