Avatar of sharingsunshine
sharingsunshine
Flag 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.

PHPWeb Development* woo commerce

Avatar of undefined
Last Comment
Chris Stanyon

8/22/2022 - Mon
Chris Stanyon

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

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

David Johnson, CD

another way is for to permanently remove it.. go to your database or in your ecom manager remove it from the categories field
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
sharingsunshine

ASKER
I actually  need the category for a php function.  I just don't need customers seeing it as a drop down.
Chris Stanyon

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 :(
sharingsunshine

ASKER
No, I am on classic not Gutenberg.  Should I use a different widget?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Chris Stanyon

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.
sharingsunshine

ASKER
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
Chris Stanyon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question