• So imagine your the consumer and this is what happens:
    1) you press space grey iPhone x
    2) You see the different variations and change space grey to silver
    3) The description says its silver and the image shows the silver version but the title still says space grey iPhone x

    So I just would like the product title to change when a specific variation is chosen.
    Thanks.

    • This topic was modified 2 years, 9 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello @reejster !!
    Are you using woocommerce or another?

    Thread Starter reejster

    (@reejster)

    @mateico I am using woocommerce yes

    I found a custom PHP code that could solve your problem. Here is the link to the article and the code.

    // Defining product Attributes term names to be displayed on variable product title
    add_filter( 'woocommerce_available_variation', 'filter_available_variation_attributes', 10, 3 );
    function filter_available_variation_attributes( $data, $product, $variation ){
        // Here define the product attribute(s) slug(s) which values will be added to the product title
        // Or replace the array with 'all' string to display all attribute values
        $attribute_names = array('Custom', 'Color');
    
        foreach( $data['attributes'] as $attribute => $value ) {
            $attribute      = str_replace('attribute_', '', $attribute);
            $attribute_name = wc_attribute_label($attribute, $variation);
    
            if ( ( is_array($attribute_names) && in_array($attribute_name, $attribute_names) ) || $attribute_names === 'all' ) {
                $value = taxonomy_exists($attribute) ? get_term_by( 'slug', $value, $attribute )->name : $value;
    
                $data['for_title'][$attribute_name] = $value;
            }
        }
        return $data;
    }
    
    // Display to variable product title, defined product Attributes term names
    add_action( 'woocommerce_after_variations_form', 'add_variation_attribute_on_product_title' );
    function add_variation_attribute_on_product_title(){
        // Here define the separator string
        $separator = ' - ';
        ?>
        <script type="text/javascript">
        (function($){
            var name = '<?php global $product; echo $product->get_name(); ?>';
    
            $('form.cart').on('show_variation', function(event, data) {
                var text = '';
    
                $.each( data.for_title, function( key, value ) {
                    text += '<?php echo $separator; ?>' + value;
                });
    
                $('.product_title').text( name + text );
    
            }).on('hide_variation', function(event, data) {
                $('.product_title').text( name );
            });
        })(jQuery);
        </script>
        <?php
    }

    Article link

    Paste the above code in your child theme’s functions.php file and should work. Also, you can add custom PHP code with a plugin like Code Snippet, here is a link to an article that explains how to usit: Code Snippet

    Let me know if it solves your problem,
    Kind regards!!

    • This reply was modified 2 years, 9 months ago by Mateo.
    Thread Starter reejster

    (@reejster)

    @mateico Nevermind I can add it but the places with // I need to add stuff right?

    The lines that start with // are comments, do not include them, they are only indicative.

    It works perfectly for me too, but I’m trying changing titles on WordPress Woocommerce shop page depends on which variation I’m using.

    It works perfectly, you can check the working code here: https://tattodivi.nhely.hu/product/flower-lily/

    I need one more solution which works on not just product page nut shop page too in this link: https://tattodivi.nhely.hu/

    Need to change little bit the actual code.
    If any of you can help me on this let me know. Thanks.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to make the title of the product change when choosing specific variation’ is closed to new replies.