Search code examples
phpjquerywoocommerceshortcodeproduct-variations

Change WooCommerce Product Name to Include Attribute When Attribute Selected


I've referenced the answer from this question which works perfectly!

But I am displaying a product on the homepage using a woocommerce shortcode and can't figure out how to adjust so it also applies to the homepage.

I tried adjusting the line that selects only product pages to include the homepage, but it does not work:

    // Only single product pages
if( ! is_product() && ! is_home() ) return $title;

Any advice would be appreciated!


Solution

  • This only work on variable products that have a Color product attribute (for variations) and it will append the selected Color attribute label value to the product title.

    For your Home page you will use WordPress conditional tag is_front_page()… You will have to set also the Product ID that you are using in your shortcode.

    1) In the Home page I have this shortcode example (with a variable product ID):

    [product_page id="40"]
    

    2) To make it work for both single product pages and the home page (product shortcode) too:

    add_filter( 'wp_footer','custom_product_title_script' );
    function custom_product_title_script(){
        // Only single product pages and home page
        if( ! ( is_product() || is_front_page() ) ) return;
    
        // HERE Set your home product ID
        $shortcode_product_id = 40; // <=====  =====  =====  HERE your Shortcode Product ID
    
        $product_id = is_product() ? get_the_id() : $shortcode_product_id;
    
        // get an instance of the WC_Product Object
        $product = wc_get_product( $product_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';
                    // 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[color]);
                                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
    }
    

    This code goes in any plugin file with a constructor (but not in function.php file).

    Tested and works.