Link to home
Avatar of sharingsunshine
sharingsunshineFlag for United States of America

asked on

How Do I Remove a Product Category from the Category Dropdown?

I want to remove a category ("california") from the category dropdown that is on my home page.  Here is the code I am using:

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 = ('4934');
      $args['exclude'] = $current_cat_id;
   }
   return $args;
}

Open in new window

But when you go to https://www.theherbsplace.com you still see the "california" category.  I have verified that is the correct id for "california.  Please tell me what I am doing wrong.

Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Hey there,

The value you pass to $args['exclude'] needs to be an array - you're not passing an array. To define an array, you either use the old way (array()), or the new ([])

// old way:
$current_cat_id = array('4934'); // notice the word 'array'

// new way 
$current_cat_id = ['4934']; // notice the square brackets

Open in new window

Avatar of sharingsunshine

ASKER

I made the change you suggested, but it is still showing see the images.  The last image is showing the category id at the bottom of the image. Here is my code
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 = ['4934'];
      $args['exclude'] = $current_cat_id;
   }
   return $args;
}

Open in new window

User generated imageUser generated image
another way is for to permanently remove it.. go to your database or in your ecom manager remove it from the categories field
I actually  need the category for a php function.  I just don't need customers seeing it as a drop down.
Hmmm- just had a quick look at this. My guess is that you're now using the Gutenberg editor to add the Product Category block. If so, then I can't see a way to filter (no apply_filters are called in the Block code!!). The code you have is for the legacy Product Category widget, not the new Gutenberg.

Seems like yet another reason to run a mile from Gutenberg - a big step backwards for developers :(
No, I am on classic not Gutenberg.  Should I use a different widget?
Ah, OK.

Just noticed in your screenshot that you're using a dropdown for the categories, so I think the hook may be a little different. Try this one instead:

woocommerce_product_categories_widget_dropdown_args

The woocommerce_product_categories_widget_args hook you're currently using is specifically for the 'list' view of categories, not the 'dropdown' one.
Still seeing it!  Here is my code, after making the change:
add_filter( 'woocommerce_product_categories_widget_dropdown_args', 'bbloomer_hide_current_cat_prod_cat_widget' );
 
function bbloomer_hide_current_cat_prod_cat_widget( $args ) {
   if ( is_product_category() ) {
      $current_cat_id = ['4934'];
      $args['exclude'] = $current_cat_id;
   }
   return $args;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial