1 回答

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超4個(gè)贊
PHP 不是這種方式,因?yàn)檫@是“客戶端” (而不是“服務(wù)器端”)的事件,因此它需要使用 jQuery。因此,要隱藏特定運(yùn)輸方式的運(yùn)輸字段部分,您將使用以下內(nèi)容:
// Auto Show hide checkout shipping fields section based on chosen shipping methods
add_action( 'wp_footer', 'custom_checkout_field_script' );
function custom_checkout_field_script() {
// Only on checkout page
if( is_checkout() && ! is_wc_endpoint_url() ):
// HERE below define your local pickup shipping method
$local_pickup = 'local_pickup:1';
// Jquery code start
?>
<script>
jQuery(function($){
var a = 'checked',
b = 'input#ship-to-different-address-checkbox',
c = 'input[name^="shipping_method"]',
d = '<?php echo $local_pickup; ?>',
e = c + ':' + a
f = 'div.woocommerce-shipping-fields,' + b;
// 1. On load: when the chosen shipping method is our defined shipping method
if( $(e).val() === d ) {
// Hide shipping fields section
$(f).hide();
}
// 2. On shipping method "change" (Live event)
$( 'form.checkout' ).on( 'change', c, function() {
// When the chosen shipping method is our defined shipping method
if( $(e).val() === d ) {
// if the checkbox is checked, uncheck it first
if ( $(b).prop(a) ) {
$(b).click();
}
// Hide shipping fields section
$(f).hide();
} else if ( $(e).val() !== d ) {
// show closed shipping fields section with (unchecked checkbox)
$(f).show();
}
});
});
</script>
<?php
endif;
}
代碼進(jìn)入您的活動(dòng)子主題(或活動(dòng)主題)的 functions.php 文件。測試和工作。
- 1 回答
- 0 關(guān)注
- 158 瀏覽
添加回答
舉報(bào)