mike
mike

Reputation: 23

Add specific product attribute after title on Woocommerce single products

In woocommerce, I am trying to include "brand name" product attributes after the product name/title or underneath in single product page as below;

Super Shark Mop - Tesco Express

I would like this brand attribute to be automatically added to title once the product is created. Any help is appreciated.

Upvotes: 2

Views: 5520

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

This is quiet easy using the following code that will remove the product title to replace it with a custom title including the product attribute "brand name" after it:

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
add_action( 'woocommerce_single_product_summary', 'custom_template_single_title', 5 );
function custom_template_single_title() {
    global $product;

    $brand_name = $product->get_attribute('brand-name');

    echo '<h1 class="product_title entry-title">';
    the_title();
    if( $brand_name )
        echo ' - ' . $brand_name;
    echo '</h1>';
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Upvotes: 5

Related Questions