【问题标题】:Change WooCommerce Product Name to Include Attribute When Attribute Selected更改 WooCommerce 产品名称以在选择属性时包含属性
【发布时间】:2018-02-15 17:38:35
【问题描述】:

我参考了this question 的答案,效果很好!

但我在主页上使用woocommerce shortcode 显示产品,但不知道如何调整,因此它也适用于主页。

我尝试调整仅选择产品页面的行以包含主页,但它不起作用:

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

任何建议将不胜感激!

【问题讨论】:

    标签: php jquery woocommerce shortcode variations


    【解决方案1】:

    这仅适用于具有 Color 产品属性的可变产品(对于变体),它将附加选定的颜色属性标签值到产品标题。

    对于您的 主页,您将使用 WordPress 条件标签 is_front_page()... 您还必须设置您在短代码中使用的产品 ID

    1) 在主页中,我有这个简码示例 (带有可变产品 ID)

    [product_page id="40"]
    

    2) 使其同时适用于单个产品页面和主页(产品短代码):

    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
    }
    

    这段代码可以放在任何带有构造函数的插件文件中(但不在 function.php 文件中)。

    经过测试并且有效。

    【讨论】:

    • 是的!非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 2020-07-20
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    相关资源
    最近更新 更多