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.
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:
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.
With a lot of help from Harshit Vaid, I have successfully solved this problem: