【问题标题】:Custom text field displayed on product page issue for WooCommerce variable productsWooCommerce 可变产品的产品页面问题上显示的自定义文本字段
【发布时间】:2018-06-12 05:08:43
【问题描述】:

我在名为“Einheitspreis”的变量下拉菜单下显示了一个自定义文本字段。它以前可以工作,但在最新更新后停止工作。 现在就像第一次打开页面时的图片一样(下拉菜单中总是有一个预先选择的值)

当在下拉列表中选择另一个值时,它会正常工作

我猜是预选值有问题

 <?php
   $custom_data = array();
   foreach ($available_variations as $prod_variation) :
    // get some vars to work with
    $variation_id = $prod_variation['variation_id'];
    $variation_object = get_post($variation_id);
    $variable_custom_field = get_post_meta( $variation_object->ID, '_text_field', true);

    $custom_data[$variation_id] = array(
        "custom_field_value" => $variable_custom_field
    );
endforeach;
?>
<?php if (!empty($variable_custom_field)) { ?>
           <span>Einheitspreis:  <span class="selected-variation-custom-field"><!-- Holds the value for the variation custom field --></span> </span>
 <?php } ?>
<script>
jQuery(function($) {
    var variation_custom_fields = <?php echo json_encode($custom_data); ?>,
        variations_data = JSON.parse( $('form.variations_form').first().attr( 'data-product_variations' ) ),
        $selected_variation_custom_field = $('.selected-variation-custom-field'); // see DIV above

    $('table.variations').on('change', 'select', function() {
        var $select = $(this),
            attribute_name = $select.attr('name'),
            selected_value = $select.val(),
            custom_field_value = "";

        // Loop over the variations_data until we find a matching attribute value
        // We then use the variation_id to get the value from variation_custom_fields
        $.each(variations_data, function() {
            if( this.attributes[ attribute_name ] &&  this.attributes[ attribute_name ] === selected_value ) {
                custom_field_value = variation_custom_fields[ this.variation_id ].custom_field_value;
                return false; // break
            }
        });

        // doing this outside the loop above ensures that the DIV gets emptied out when it should
        $selected_variation_custom_field.text( custom_field_value );
    });
});
</script>

【问题讨论】:

  • 看起来您正在执行更改功能,默认情况下在页面加载时不会触发,因为它不会“更改”。您是否尝试过使用$('table.variations select').change(); 之类的东西来触发更改。让我知道这是否有效。
  • 您是否更改了此处给出的设置? docs.woocommerce.com/document/variable-product/…

标签: php jquery wordpress woocommerce variations


【解决方案1】:

您需要在加载 DOM 时首先执行代码,并且在选择属性值时也需要执行此操作。因此,为了使您的代码可重用(对于这 2 个事件),我们将其设置在一个函数中:

<?php
   $custom_data = array();
   foreach ($available_variations as $prod_variation) :
    // get some vars to work with
    $variation_id = $prod_variation['variation_id'];
    $variation_object = get_post($variation_id);
    $variable_custom_field = get_post_meta( $variation_object->ID, '_text_field', true);

    $custom_data[$variation_id] = array(
        "custom_field_value" => $variable_custom_field
    );
endforeach;

if (!empty($variable_custom_field)) { ?>
           <span>Einheitspreis:  <span class="selected-variation-custom-field"><!-- Holds the value for the variation custom field --></span> </span>
 <?php } ?>
<script>
    jQuery(function($) {
        var variation_custom_fields = <?php echo json_encode($custom_data); ?>,
            variations_data = JSON.parse( $('form.variations_form').first().attr( 'data-product_variations' ) ),
            $selected_variation_custom_field = $('.selected-variation-custom-field'); // see DIV above

        // We set everything in a reusable function 
        function getSelectOnChange( $select ){
            var attribute_name = $select.attr('name'),
                selected_value = $select.val(),
                custom_field_value = "";

            // Loop over the variations_data until we find a matching attribute value
            // We then use the variation_id to get the value from variation_custom_fields
            $.each(variations_data, function() {
                if( this.attributes[ attribute_name ] &&  this.attributes[ attribute_name ] === selected_value ) {
                    custom_field_value = variation_custom_fields[ this.variation_id ].custom_field_value;
                    return false; // break
                }
            });

            // doing this outside the loop above ensures that the DIV gets emptied out when it should
            $selected_variation_custom_field.text( custom_field_value );
        }

        // 1. AT START (Once Dom is loaded)
        $('select').each( function() {
            if ($(this).val() != '' )
                getSelectOnChange( $(this) );
        });

        // 2. WHEN SELECTING ATTRIBUTE VALUES (Live)
        $('table.variations').on('change', 'select', function() {
            getSelectOnChange( $(this) );
        });
    });
</script>

此代码已经过测试并且可以工作...它显示默认选择字段值(默认变体)的自定义字段值...

【讨论】:

    【解决方案2】:

    这应该适合你。

    基本上,您可以在函数内部获取逻辑。然后在 DOMReady 上执行该函数一次,然后将其与更改事件绑定。

    <script>
    jQuery(function($) {
        var variation_custom_fields = <?php echo json_encode($custom_data); ?>,
            variations_data = JSON.parse( $('form.variations_form').first().attr( 'data-product_variations' ) ),
            $selected_variation_custom_field = $('.selected-variation-custom-field'); // see DIV above
    
        var updateUnitPrice = function() {
            var $select        = $(this),
                attribute_name = $select.attr('name'),
                selected_value = $select.val(),
                custom_field_value = "";
    
            // Loop over the variations_data until we find a matching attribute value
            // We then use the variation_id to get the value from variation_custom_fields
            $.each(variations_data, function() {
                if( this.attributes[ attribute_name ] &&  this.attributes[ attribute_name ] === selected_value ) {
                    custom_field_value = variation_custom_fields[ this.variation_id ].custom_field_value;
                    return false; // break
                }
            });
    
            // doing this outside the loop above ensures that the DIV gets emptied out when it should
            $selected_variation_custom_field.text( custom_field_value );
        };
    
        $('table.variations select').updateUnitPrice();
    
        $('table.variations').on('change', 'select', function() {
            $( this ).updateUnitPrice();
        });
    });
    </script>
    

    【讨论】:

      猜你喜欢
      • 2020-12-04
      • 1970-01-01
      • 2019-01-09
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      • 2019-06-26
      • 2020-02-22
      • 1970-01-01
      相关资源
      最近更新 更多