Change Woocommerce Product Categories Widget ‘Title’ based on Product Category

  • Your code has syntax error if and else block are not closed!
  • Also you are changing all widget title. Be specific to target only product category widget by checking for widget ID
  • Your current code will only effect if you are viewing single product page belongs to above categories. For all other pages return title as it is so title will not be blank.

Example:-

function retitle_woo_category_widget($title, $widet_instance, $widget_id) {

    if ( $widget_id !== 'woocommerce_product_categories' )
        return $title;

    // If 'Category' 1 is being viewed...
    if ( is_product() && has_term( 'Category 1', 'product_cat' ) ) {
        //... Remove the images
        return __('Title of Widget for Category 1');

    // If 'Category' 2 is being viewed...
    } else if ( is_product() && has_term( 'Category 2', 'product_cat' ) ) {
        return __('Title of Widget for Category 2');
    }

    return $title;
}
add_filter ( 'widget_title' , 'retitle_woo_category_widget', 10, 3);