1

In WooCommerce I am trying to add "pa_artist" product attribute before the product title in single product page as below:

pa_attribute – product title

I would like the "artist" product attribute term name to be automatically listed before the title. I managed to add attribute to product title using this answer code.

What I need is to add an active link to the "artist" product attribute term name, that display the products for this attribute term name.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Solarside
  • 23
  • 3
  • _"..I managed to add attribute to product title.."_ please share the code you used for this. Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [Stack Overflow question checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). Then write your question with details to create [a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – 7uc1f3r Oct 09 '20 at 09:51
  • 1
    OK, thanks for the detail @LoicTheAztec. – halfer Oct 12 '20 at 20:40

1 Answers1

1

Based on Add specific product attribute after title on Woocommerce single products you can easily change the code to add a specific product attribute before the product title on WooCommerce single product pages as follows:

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;

    $taxonomy     = 'pa_artist';
    $artist_terms = get_the_terms( $product->get_id(), $taxonomy ); // Get the post terms array
    $artist_term  = reset($artist_terms); // Keep the first WP_term Object
    $artist_link  = get_term_link( $artist_term, $taxonomy ); // The term link

    echo '<h1 class="product_title entry-title">';

    if( ! empty($artist_terms) ) {
        echo '<a href="' . $artist_link . '">' . $artist_term->name . '</a> - ';
    }

    the_title();

    echo '</h1>';
}

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


Addition: For multiple linked product attributes terms use the following instead:

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;

    $taxonomy     = 'pa_artist';
    $artist_terms = get_the_terms( $product->get_id(), $taxonomy ); // Get the WP_terms array for current post (product)
    $linked_terms = []; // Initializing

    // Loop through the array of WP_Term Objects
    foreach ( $artist_terms as $artist_term ) {
        $artist_link    = get_term_link( $artist_term, $taxonomy ); // The term link
        $linked_terms[] = '<a href="' . $artist_link . '">' . $artist_term->name . '</a>';
    }
    if( ! empty($linked_terms) ) {
       echo '<h1 class="product_title entry-title">' . implode( ' ', $linked_terms) . ' - ';
       the_title();
       echo '</h1>';
    }
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks for your input, LoicTheAztec! The code you provided adds attribute to the product title, but i need it to be posted as an attribute with active link just before the title like in example in my initial post. – Solarside Oct 09 '20 at 12:21
  • thanks, it does the job! Cuuld you kindly help to make it work when product has several attribute values, like this **value 1 / value 2 - product title**? – Solarside Oct 12 '20 at 16:58
  • @Solarside I have added an addition in my answer. – LoicTheAztec Oct 12 '20 at 19:00