问题描述
我使用的是我测试过的raise_error答案代码,效果很好。
但是问题在于,它隐藏了所有国家/地区的运费计算器的邮政编码。
我想对除一个国家(比利时)以外的所有国家都隐藏它。
这可能吗?我如何在除比利时以外的所有国家/地区使用它。
解决方法
我认为您可以使用某些jQuery根据所选国家/地区动态隐藏和显示邮政编码字段
add_action( 'wp_footer','show_shipping_calculator_postcode_field_based_on_country',50 );
function show_shipping_calculator_postcode_field_based_on_country() {
if ( ! is_cart() ) return;
?>
<script type='text/javascript'>
jQuery(function($){
$(document.body).on('change','select[name="calc_shipping_country"]',function() {
let country = $(this).find( 'option:selected' ).val();
let postcode = $(this).closest( 'p#calc_shipping_country_field' ).siblings( 'p#calc_shipping_postcode_field' ).find('input');
if ( country !== 'BE' ) {
postcode.prop('disabled',true);
postcode.attr('value','');
postcode.hide();
} else {
postcode.prop('disabled',false);
postcode.attr('value','<?php echo WC()->customer->get_shipping_postcode(); ?>');
postcode.show();
}
});
});
</script>
<?php
}
这将清除邮政编码字段的值,将其禁用,并在所选国家/地区不是比利时时将其隐藏。