search

Home  >  Q&A  >  body text

Show only Woocommerce categories with products with a specific meta value

I'm developing the b2b part of my Woocommerce store. I have successfully filtered woocommerce_product_query_meta_query to only show products that have the b2b part enabled to b2b users.

However, I can't find a way to hide a product category that shows 0 results in the Woocommerce categories widget (because there are no products in that category with the b2b part enabled).

I considered rewriting the default Woocommerce widget code and running a wp query for each category (and subcategory) that returns the number of products in that category with b2b enabled. But for a large number of products and categories this seems very inefficient.

Is there a way to hide "empty" categories (no b2b enabled products in the category) in the Woocommerce categories widget?

Thanks for any suggestions.

edit

To clarify my question: This is the function I use to filter the product query to only show products that have the _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 );

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

This category is not empty for ordinary customers and non-logged in users. But for b2b customers, there are no products to display. So I need to hide this category widget for b2b customers.

P粉037215587P粉037215587320 days ago420

reply all(2)I'll reply

  • P粉466909449

    P粉4669094492024-01-11 14:57:57

    If you are referring to the product categories widget, there is a setting to hide empty categories:

    If you are referring to something else, please provide a URL to a sample page and the system status of your site. You can find it via WooCommerce > Status. Select Get System Report and then Copy for Support. Once completed, please paste it in your reply.

    Hope this helps.

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

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

    //* 当小部件以下拉列表显示时使用
        add_filter( 'woocommerce_product_categories_widget_dropdown_args', 'rv_exclude_wc_widget_categories' );
        //* 当小部件以列表显示时使用
        add_filter( 'woocommerce_product_categories_widget_args', 'rv_exclude_wc_widget_categories' );
        function rv_exclude_wc_widget_categories( $cat_args ) {
        //添加逻辑来检查类别是否有产品,并创建ID数组,并用该数组替换下面的数组。
            $cat_args['exclude'] = array('55','68'); // 插入您希望排除的产品类别ID
            return $cat_args;
        }

    In the above code, I think you can make the logic and check if the category has products and create an array of IDs for non-product categories.

    This way you can exclude the category from lists and dropdowns.

    Hope this helps.

    reply
    0
  • P粉766520991

    P粉7665209912024-01-11 10:00:56

    With a lot of help from Harshit Vaid, I have successfully solved this problem:

    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;
    }

    reply
    0
  • Cancelreply