Woocommerce Product Category Widget – hide categories that have no products in stock [closed]

I’m using WooCommerce on my WordPress site, and have a widget in the sidebar showing all product categories. It’s setup to hide empty categories, and that works well. However, it still shows categories that contain only products that are out of stock – that I would like to change.

Technical explanation: WooCommerce products are a custom post type (product), the product categories are a custom taxonomy (product_cat), and the stock count is a custom field on the products (_stock). The WooCommerce Product Categories widget shows them using wp_list_categories(). I have tried digging into a code to look for some filter call that I could hook onto, but failed to find one.

Is there a good way of doing this without modifying core or WooCommerce code? Maybe a hook/filter somewhere that I missed that would allow to check the custom field value?

3 Answers
3

Use the woocommerce_product_categories_widget_args and woocommerce_get_availability filters in a custom function to filter the product categories widget for out of stock products.

See what you can come up using these filters and post the code back here if you get stuck.

Here’s some code from my site which may help you get started.

add_filter( 'woocommerce_product_categories_widget_args', 'wpsites_exclude_product_cat_widget' );

function wpsites_exclude_product_cat_widget( $args ) {

$args['exclude'] = array('16','46');

return $args;
}

Here’s all the hooks for WooCommerce

Leave a Comment