我在名为" 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>
答案 0 :(得分: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>
此代码经过测试并有效...它显示默认选择字段值的自定义字段值(默认变体)...
答案 1 :(得分: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>