0

I'm really not a developer, but this is something I expect not to be too hard to achieve :-) I really don't know where to start, so that's point 1. I read all of the topics about dynamically changing the widget title, but they all need human interaction.

Maybe I'm thinking too simple, but wouldn't it be easy to replace the default WOOCommerce Widget title (ie. "Product categories") by the parent category title?

Let's say that I'm browsing through the category "Nike" where I'm viewing products or categories, and the sub-categories are showing in the widget (ie. Shoes, Shirts, etc.). In this case the widget title "Product categories" should show "Nike" as widget title.

Could you help me getting it up and running?

2 Answers 2

1

Updated for 2022:

//Customize Woocommerce Category Widget to show title of current category
function dynamic_product_category_widget($title, $widget_instance, $widget_id) {

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

    if ( is_product_category() ) {
        $cat_title = '<p class="widget-title footer-widget">'.single_cat_title()."</p>";
        return $cat_title;
    } elseif ( is_product() ) {
        $cat = get_the_terms( $product->ID, 'product_cat' );

        foreach ($cat as $categoria) {
            if($categoria->parent == 0){
                $topCategory = $categoria;
            }
        }
        return '<p class="widget-title footer-widget">'.$topCategory->name.'</p>';
    }
    
    return $title;
}
add_filter ( 'widget_title' , 'dynamic_product_category_widget', 10, 3);
0

Try this it works for me. Place this code in your theme functions.php file.

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

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

    if ( is_product_category() ) {
        $cat_title = single_cat_title('<p class="widget-title footer-widget">', '</p>');
        return $cat_title;
    } elseif ( is_product() ) {
        $cat = get_the_terms( $product->ID, 'product_cat' );

        foreach ($cat as $categoria) {
            if($categoria->parent == 0){
                $topCategory = $categoria;
            }
        }
        return '<p class="widget-title footer-widget">'.$topCategory->name.'</p>';
    }
    
    return $title;
}
add_filter ( 'widget_title' , 'dynamic_product_category_widget', 10, 3);

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.