Home » WooCommerce: Hide Categories From Product Cat Widget

WooCommerce: Hide Categories From Product Cat Widget

by Tutor Aspire

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-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 tutoraspire.com FREE
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 3.9
 * @donate $9     https://tutoraspire.com
 */

add_filter( 'woocommerce_product_categories_widget_args', 'tutoraspire_hide_current_cat_prod_cat_widget' );

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

You may also like