【问题标题】:Show shipping calculator postcode field only for specific countries in WooCommerce仅在 WooCommerce 中显示特定国家/地区的运费计算器邮政编码字段
【发布时间】:2020-12-27 04:54:32
【问题描述】:
【问题讨论】:
标签:
php
wordpress
woocommerce
cart
shipping
【解决方案1】:
我认为您可以使用一些 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
}
这会清除邮政编码字段的值,禁用它并在所选国家不是比利时时隐藏它。