0

I have the following function which I need to apply on a particular category only:

function ChangeSelectTitle($woocommerce_product_categories_widget_args){
$woocommerce_product_categories_widget_args['show_option_none'] = __('Select your Device/Brand');
return $woocommerce_product_categories_widget_args;
}

if( $product_cat_id == 6 ) {
add_filter('woocommerce_product_categories_widget_dropdown_args', 'ChangeSelectTitle');
}

The category ID is 6. I have tried putting $product_cat_id == 6to identify the category page but it's not working. How to make it work?

6
  • Where is this code used and where are you getting the variable $product_cat_id from?
    – Peter HvD
    Commented Aug 13, 2018 at 10:26
  • This code is being used in the functions file of the active theme and got the variable by seeing the code of another similar plugin.
    – dc09
    Commented Aug 13, 2018 at 10:34
  • By "where are you getting the variable from" I meant where in your code is the variable defined?
    – Peter HvD
    Commented Aug 13, 2018 at 10:40
  • It's not defined, I used it assuming it's a universal variable for wordpress.
    – dc09
    Commented Aug 13, 2018 at 10:47
  • Well there's part of your problem then, because it isn't a global variable. Even if it was, you would have to invoke it using global $product_cat_id; before you could use it. Anyway, I suspect you're barking up the wrong tree. The widget args are set before it builds the category list, so you can't hope to effect it like that. Also, the text you're looking to change is displayed when there are no categories to display. The original text is "No product categories exist", so changing it to "Select your Device/Brand" doesn't make sense to me as there's nothing to select!
    – Peter HvD
    Commented Aug 13, 2018 at 10:53

1 Answer 1

1

You're trying to use $product_cat_id even though it isn't defined anywhere. If you want to know the current category being viewed you need to use is_product_category(6), but you need to use that inside the callback function (ChangeSelectTitle()), because WordPress hasn't determined whether it's a category archive yet when it runs functions.php.

function ChangeSelectTitle($woocommerce_product_categories_widget_args) {
    if ( is_product_category(6) ) {
        $woocommerce_product_categories_widget_args['show_option_none'] = __('Select your Device/Brand');
    }

    return $woocommerce_product_categories_widget_args;
}
add_filter('woocommerce_product_categories_widget_dropdown_args', 'ChangeSelectTitle');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.