0

I found this code to show attribute after a title: Add specific product attribute after title on Woocommerce single products

    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>';
}

is work but, i try to show only one the selected attribute to appear.

An example attribute from color: white and black

The code works like

"Product Title - White, Color"

What i want to show is

if not selected any attribute

"Product Title"(not to appear anything)

if selected white, show like

"Product Title - White"

Uxell
  • 35
  • 5
  • Works, but not the best implementation, and your question is: you don't want the title and only the attribute?, if that then you can remove the_title(); from the code – Cristino Nov 04 '21 at 18:51
  • @Uxell Please explain more, or at least provide a screenshot to clarify your question. – Ruvee Nov 04 '21 at 19:01
  • in this code show an example: attribute color has blue white and green show all of them like: "Product Title - blue, white, green", what i want to do is to show only selected attribute, like selected is white "Product Title - white" – Uxell Nov 05 '21 at 09:13
  • @Ruvee I edited the question to explain clearly. – Uxell Nov 05 '21 at 10:48

1 Answers1

1

Add the code in your functions.php . Replace pa_color with your attribute

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');
   $brand_output = '';
   if( $brand_name )
       $brand_output = ' - ' . $brand_name;

   echo sprintf('<h1 class="product_title entry-title">%s %s <span></span></h1>',get_the_title(),$brand_output);
}

function change_title_on_color_change() { 
    global $product;
    if($product->get_type() !== 'variable') return;    
?>
<script>
jQuery(function($) {

    $(document).ready(function() {
        update_product_title();
    });
    $('select#pa_color').change( function(){
        update_product_title();
    });
    function update_product_title() {
        var color_val = $('select#pa_color option').filter(':selected').val();
        var color_text = $('select#pa_color option').filter(':selected').text();
        if(color_val.length > 0) {
            $('.product_title.entry-title span').text(' - ' + color_text);
        } else {
            $('.product_title.entry-title span').empty();
        }
    }
});
</script>
<?php
}
add_action('woocommerce_after_single_product','change_title_on_color_change');
Snuffy
  • 1,723
  • 1
  • 5
  • 9
  • thanks for sharing, but not working changed the 3 #pa_color with attribute slug not working @Martin – Uxell Nov 05 '21 at 13:11
  • If your attribute group is Color should be pa_color. Not slugs :) – Snuffy Nov 05 '21 at 13:12
  • Thank you sir i leave your clean code is color but still not working – Uxell Nov 05 '21 at 13:15
  • added space on first 2 line before ,5 and before ,100 like , 5 and , 100 is worked but the title duplicated – Uxell Nov 05 '21 at 13:18
  • Are you still using your actions ? If you want to use your actions at echo ''; add the span e.g echo '';. Also check this - https://prnt.sc/1yfd54o – Snuffy Nov 05 '21 at 13:19
  • thanks for the trick via screenshot but as i mention before the title got duplicate the one with Like what i want "Product Title - xCOLORx" but from bottom "Product Title" – Uxell Nov 05 '21 at 13:22
  • I have updated my answer with your actions. – Snuffy Nov 05 '21 at 13:24
  • I did as your last code is what I want but the only thing the title get duplicated https://prnt.sc/1yfdy7f – Uxell Nov 05 '21 at 13:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/238907/discussion-between-uxell-and-martin-mirchev). – Uxell Nov 05 '21 at 13:42
  • I have updated my answer and iam in chat – Snuffy Nov 05 '21 at 13:46