2

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
mike
  • 23
  • 1
  • 4

1 Answers1

5

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hello, many thanks for your assistance. It doesn't work for some reason. It duplicates the title but still doesn't add the brand at the end of it. See screenshot here - [link](https://www.bourbonapples.co.uk/wp-content/uploads/2018/07/screenshot-www.bourbonapples.co_.uk-2018.07.17-11-14-52.png) – mike Jul 17 '18 at 10:17
  • Hello @LoicTheAztec the script you helped me with isn't working. Any chance of a fix please? – mike Aug 29 '18 at 23:53
  • it still doesn't show the brand name and also it only duplicates the title. Please see link in comment above. – mike Aug 31 '18 at 08:31
  • Hello @LoicTheAztec it's an attribute not using a plugin for brands. Basically, I will like to append the brand name to product titles. So on existing product titles, I just need a code to add in order to **show the brand names automatically at the end of the product titles** e.g 50L Water Filter System - Dyson – mike Sep 01 '18 at 15:49
  • Hello @LoicTheAztec many thanks for the script above. It works now. The issue was the attribute name. I used **"brand"** rather than **"brand name"** in the product editor. However, it still doesn't remove the existing title. Only creates a new title with the attribute affixed, below the product price. Could you assist with this please? Thanks in advance. – mike Dec 27 '18 at 20:57
  • Please see example here - [link](https://www.bourbonapples.co.uk/wp-content/uploads/2018/12/example.png) And result here - [link](https://www.bourbonapples.co.uk/wp-content/uploads/2018/12/result.png) – mike Dec 27 '18 at 22:04
  • answer accepted, just needs some tweaking which I need your help with. The **remove_action** isn't working and the **add_action** adds title to the wrong place, see images in link above. – mike Dec 27 '18 at 22:34
  • Hi, the code doesn't remove the old title. – Helen_zs Feb 05 '21 at 11:26
  • @Helen_zs The code is not made to replace the title, but just to add something to it… – LoicTheAztec Feb 05 '21 at 11:34
  • I understand that, but it displays: first the title with the attribute (witch is perfect) and bellow the original title without attribute.(this shouldn't be displayed). – Helen_zs Feb 05 '21 at 11:38
  • You are right! The theme had customization on title. You are the best here!! Thanks! *Any chance to make it show on archive pages too? – Helen_zs Feb 05 '21 at 12:28