A client asked me to customize the Product Categories Widget that comes included with WooCommerce. In this case, they wanted to show ALL categories but the current one (when looking at a category page of course).

You can also use this tutorial to hide certain category IDs from the widget, or maybe “all product category names that start with a given string of text”. Case studies are millions, but the code base is always the same – we’ll make use of the “woocommerce_product_categories_widget_args” filter and provide a list of product categories to exclude. Enjoy!

woocommerce-hide-non-current-categories-widgetwoocommerce-hide-non-current-categories-widget
WooCommerce case study: hide current product category from the Product Categories Widget

PHP Snippet: Hide Current Category From The WooCommerce Product Categories Widget

/**
 * @snippet       Hide Current Category @ WooCommerce Prod Cat Widget
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.9
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_filter( 'woocommerce_product_categories_widget_args', 'bbloomer_hide_current_cat_prod_cat_widget' );

function bbloomer_hide_current_cat_prod_cat_widget( $args ) {
   if ( is_product_category() ) {
      $current_cat_id = get_queried_object_id();
      $args['exclude'] = $current_cat_id;
   }
   return $args;
}
Share:

Leave a Reply