WooCommerce变量产品的产品页面问题上显示的自定义文本字段

问题描述 投票:1回答:2

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

enter image description here

当在下拉列表中选择另一个值时,它应该像enter image description here一样工作

我想预先选择的值存在问题

 <?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>
php jquery wordpress woocommerce variations
2个回答
1
投票

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

<?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>

此代码经过测试并正常运行...它显示默认选择字段值的自定义字段值(默认变体)...


0
投票

这应该适合你。

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

<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>
© www.soinside.com 2019 - 2024. All rights reserved.