【发布时间】: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