Home › Forums › Themes Support › Claue › Change WooCommerce product title based on variation selected
- This topic has 5 replies, 2 voices, and was last updated 5 years, 4 months ago by Harry.
-
AuthorPosts
-
-
August 5, 2019 at 10:19 pm #32934DanielJohnstonSupport Expired
Hi
I would like to be able to show the product variation on the product title.
Variation is colour
Product is shoes
So when someone views the product and clicks the red swatch the product title (product_title entry-title) would read shoes : red
-
August 5, 2019 at 11:06 pm #32938HarrySupport Expired
Hi,
Please add below code to claue-child > functions.php
add_filter( 'wp_footer','custom_product_title_script' ); function custom_product_title_script(){ global $post; // Only single product pages if( ! is_product() ) return; // get an instance of the WC_Product Object $product = wc_get_product($post->ID); // Only for variable products if( ! $product->is_type( 'variable' ) ) return; // Here set your specific product attributes in this array (coma separated): $attributes = array('pa_color'); // The 1st loop for variations IDs foreach($product->get_visible_children( ) as $variation_id ) { // The 2nd loop for attribute(s)/value foreach($product->get_available_variation( $variation_id )['attributes'] as $key => $value_id ){ $taxonomy = str_replace( 'attribute_', '', $key ); // Get the taxonomy of the product attribute // Just for defined attributes if( in_array( $taxonomy, $attributes) ){ // Set and structure data in an array( variation ID => product attribute => term name ) $data[ $variation_id ][$taxonomy] = get_term_by( 'slug', $value_id, $taxonomy )->name; } } } ?> <script type="text/javascript"> (function($){ // variables initialization var variationsData = <?php echo json_encode($data); ?>, productTitle = $('.product_title').text(), color = 'pa_color'; console.log(variationsData); // function that get the selected variation and change title function update_the_title( productTitle, variationsData, color ){ $.each( variationsData, function( index, value ){ if( index == $('input.variation_id').val() ){ $('.product_title').text(productTitle+' - '+value); console.log('TITLE UPDATED'); return false; } else { $('.product_title').text(productTitle); } }); } // Once all loaded setTimeout(function(){ update_the_title( productTitle, variationsData, color ); }, 300); // On live event: select fields $('select').blur( function(){ update_the_title( productTitle, variationsData, color ); }); })(jQuery); </script> <?php }
Kind regards
Harry
Premium Wordpress themes and plugins, Best WooCommerce theme https://themeforest.net/user/janstudio/portfolio?ref=janstudio -
August 6, 2019 at 12:34 am #32940DanielJohnstonSupport Expired
Thank you, I found the same code.
How do I find the // Here set your specific product attributes in this array (coma separated):
?Did this code work for you?
-
August 7, 2019 at 6:23 pm #32981HarrySupport Expired
Hi,
Sorry for late reply. Because your variation are colour so please change code to
add_filter( 'wp_footer','custom_product_title_script' ); function custom_product_title_script(){ global $post; // Only single product pages if( ! is_product() ) return; // get an instance of the WC_Product Object $product = wc_get_product($post->ID); // Only for variable products if( ! $product->is_type( 'variable' ) ) return; // Here set your specific product attributes in this array (coma separated): $attributes = array('pa_colour'); // The 1st loop for variations IDs foreach($product->get_visible_children( ) as $variation_id ) { // The 2nd loop for attribute(s)/value foreach($product->get_available_variation( $variation_id )['attributes'] as $key => $value_id ){ $taxonomy = str_replace( 'attribute_', '', $key ); // Get the taxonomy of the product attribute // Just for defined attributes if( in_array( $taxonomy, $attributes) ){ // Set and structure data in an array( variation ID => product attribute => term name ) $data[ $variation_id ][$taxonomy] = get_term_by( 'slug', $value_id, $taxonomy )->name; } } } ?> <script type="text/javascript"> (function($){ // variables initialization var variationsData = <?php echo json_encode($data); ?>, productTitle = $('.product_title').text(), color = 'pa_colour'; console.log(variationsData); // function that get the selected variation and change title function update_the_title( productTitle, variationsData, color ){ $.each( variationsData, function( index, value ){ if( index == $('input.variation_id').val() ){ $('.product_title').text(productTitle+' - '+value); console.log('TITLE UPDATED'); return false; } else { $('.product_title').text(productTitle); } }); } // Once all loaded setTimeout(function(){ update_the_title( productTitle, variationsData, color ); }, 300); // On live event: select fields $('select').blur( function(){ update_the_title( productTitle, variationsData, color ); }); })(jQuery); </script> <?php }
Kind regards
Harry
Premium Wordpress themes and plugins, Best WooCommerce theme https://themeforest.net/user/janstudio/portfolio?ref=janstudio -
August 7, 2019 at 10:36 pm #32991DanielJohnstonSupport Expired
Thank you for your help so far
The title now changes to – [object Object]
-
August 7, 2019 at 11:17 pm #32994HarrySupport Expired
Can you send me login credentials and check in “Set as private reply” We need to check more before give you solution.
Kind regards
Harry
Premium Wordpress themes and plugins, Best WooCommerce theme https://themeforest.net/user/janstudio/portfolio?ref=janstudio
-
-
AuthorPosts
You must be logged in to reply to this topic.