【问题标题】:Change WooCommerce Product Name Include Color When Attribute Selected选择属性时更改 WooCommerce 产品名称包括颜色
【发布时间】:2020-02-12 17:23:06
【问题描述】:

我正在寻找可以帮助我根据颜色更改 WooCommerce 可变产品标题的人。在这种特定情况下,我希望在选择颜色时更改标题,例如“产品名称 - 黑色”。

我关注这个主题Change WooCommerce variable product title based on variation,但它不适用于 WooCommerce 版本 3.9.1

和代码

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[color]);
                            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
}

【问题讨论】:

    标签: jquery woocommerce variations


    【解决方案1】:

    我尝试了新代码,它适用于新版本 3.9.2

    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[color]);
                                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 );
                    // });
    
                    $('#pa_colour').on('change', function(){
                       // update_the_title( productTitle, variationsData, color );
                        setTimeout(function(){
                          update_the_title( productTitle, variationsData, color );
                        }, 300);
                    });
    
                })(jQuery);
            </script>
        <?php
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-10
      • 2020-07-20
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 2022-01-16
      • 2020-11-07
      相关资源
      最近更新 更多