根据属性值选择更改可变woocommerce产品的标题

时间:2020-09-24 14:51:16

标签: wordpress woocommerce

当某人选择变量时,我想更改Woocommerce变量产品标题。我想将选定的变体添加到标题。 我已经使用了这段Change WooCommerce variable product title based on variation代码,它非常适合从product-> attributes添加的属性。但是,我希望它可以用于添加产品时添加的自定义产品属性。<​​/ p>

我为此尝试了上面链接中提供的代码,但是不适用于自定义产品属性,它返回一个空值。 colorr是我使用自定义产品属性添加的属性名称 这是我的代码。任何帮助都会得到高度重视

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('colorr');

    // 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 = 'colorr';
                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
}

1 个答案:

答案 0 :(得分:0)

不确定为什么使用php,这完全是前端任务-因此应使用前端js完成。
请将此js添加到外部javascript文件(这样会导致不好注入JS inline ,尤其是jquery的做法-它会阻止defer optimization ) 运作方式:

  1. 将默认标题文本获取为var
  2. 收集所有选择的变体并添加到默认标题文本
  3. 将新文本设置为标题
jQuery( document ).ready( function( $ ) {
    var title_text = $( '.product-type-variable .product_title' ).text(); // get default title
    function add_variation_txt_to_title() {
        var new_title_text = '';
        $( '.variations select' ).each( function() {
            new_title_text += ' ' + $( this ).find( ':selected' ).val(); // collect all variation selected <options>
        })
        $( '.product-type-variable .product_title' ).text( title_text + new_title_text ); // set new title
    }
    add_variation_txt_to_title(); // call on load
    $( '.variations select' ).change( function() { // call on <select >change 
        add_variation_txt_to_titl();
    })
})

该解决方案针对默认的27个主题进行了测试。