如何解决仅显示WooCommerce中特定国家/地区的货运计算器邮政编码字段
我使用的是我测试过的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
}
这将清除邮政编码字段的值,将其禁用,并在所选国家/地区不是比利时时将其隐藏。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。