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
With a big help from Harshit Vaid I have managed to solve it:
If you are referring to the product categories widget, there is a setting for hiding empty categories:
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
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.