• Resolved thundernut

    (@thundernut)


    Hello, I have built woocommerce website using woodmart theme. As some of you may know, you can’t give unique title to variations of variable products.
    I’m looking for a plugin that will allow me to give names to my variations, for example, if I have variable product named Caucasus maps with the attributes English/Georgian, Political/Physical, etc. I would want to change the name of the product when variation is selected. From Caucasus maps to Political map of Caucasus – ENG or something else.

    In short, I want to be able to give names to my variations and when one of them is selected, title of main product should change.

    I was looking for plugins with that functionality, but couldn’t really find one. Doesn’t matter if it’s free or not. If there is any, that is stable ( meaning gets updates time to time and is compatible with current wordpress) please suggest me.

    Thanks in advance!

    • This topic was modified 1 year, 5 months ago by thundernut.
Viewing 1 replies (of 1 total)
  • Hi @thundernut

    Upon checking on the internet, I found a custom PHP code that might do the job for you, here is the code and the link to the article:

    Link: https://stackoverflow.com/questions/47462265/add-some-attribute-values-to-woocommerce-variable-product-title-from-chosen-vari?answertab=active#tab-top

    // 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
    }

    I normally recommend using the Code Snippet plugin in order to add custom PHP code into your site without having to touch the functions.php file. Here is a link on how to use the Code Snippet plugin:

    https://www.wpbeginner.com/plugins/how-to-easily-add-custom-code-in-wordpress-without-breaking-your-site/

    Let me know if that helps!

Viewing 1 replies (of 1 total)
  • The topic ‘Display variation name Instead of/besides the product title when selected’ is closed to new replies.