php [改变基于产品标题的变化]改变基于产品标题的变化|防止取消选择变种#wordpress

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [改变基于产品标题的变化]改变基于产品标题的变化|防止取消选择变种#wordpress相关的知识,希望对你有一定的参考价值。

<?php
/*
https://stackoverflow.com/questions/47462265/change-woocommerce-variable-product-title-based-on-variation
https://yonkov.github.io/post/change-product-title-based-on-product-variations-in-woocommerce/

plugin: https://wordpress.org/plugins/force-default-variant-for-woocommerce/
*/
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','pa_chon-mau','pa_dung-luong');

    // 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(),
                    attrs = ['pa_chon-mau','pa_dung-luong'];
                console.log(variationsData);

                // function that get the selected variation and change title
                function update_the_title( productTitle, variationsData, attrs ){
                    $.each( variationsData, function( index, value ){
                        if( index == $('input.variation_id').val() ){
                          	var suffix='';
							for(var i in attrs) suffix+= value[attrs[i]]+', ';
							suffix = $.trim(suffix).replace(/,$/g,'');
                          
                            $('.product_title').text(productTitle+' - ('+suffix+')');
                            console.log('TITLE UPDATED');
                            return false;
                        } else {
                            $('.product_title').text(productTitle);
                        }
                    });
                  	//change price
					var price = $('.woocommerce-variation-price .woocommerce-Price-amount').text();
					if(price) $('.product-page-price .woocommerce-Price-amount').text(price);
                }

                // Once all loaded
                setTimeout(function(){
                    update_the_title( productTitle, variationsData, attrs );
                }, 300);

                // On live event: select fields
                $('select').blur( function(){
                    update_the_title( productTitle, variationsData, attrs );
                });
              
              /**
               * prevent unselect variation
              */
              $('select').on('change', function(){
					var c=$(this),v=c.val();
					if(v ) c.data('value',v);
					if(!v) c.val(c.data('value'));
					console.log(22, $(this).val());
				});
				$('select').each(function(){
					var v = $(this).val();
					if(v) $(this).data('value', v);
				});
              
            })(jQuery);
        </script>
	<style>
      .reset_variations{display:none;}
	</style>
    <?php
}

以上是关于php [改变基于产品标题的变化]改变基于产品标题的变化|防止取消选择变种#wordpress的主要内容,如果未能解决你的问题,请参考以下文章

如何改变WooCommerce的商品列表样式

基于 ANSIBLE 自动化运维实践

产品价格后缀,基于WooCommerce的重量计算价格

php 为Betheme改变WooCommerce单一产品页面位置,用于XL WooCommerce销售触发插件

产品导入后产品属性不会改变

公有云上基于微服务架构SAAS产品研发实践「活动通知」