上述问题的答案帮助了我,但我仍然有一个问题:Show hide custom Woocommerce checkout field based on selected payment method
我希望当客户 Select cheque支付网关时显示电话字段(需要一个字段),如果他 Select 其他支付网关,则不显示和禁用移动字段.
// Conditional Show hide checkout fields based on chosen payment methods
add_action( 'wp_footer', 'conditionally_show_hide_billing_custom_field' );
function conditionally_show_hide_billing_custom_field(){
// Only on checkout page
if ( is_checkout() && ! is_wc_endpoint_url() ) :
?>
<script>
jQuery(function($){
var a = 'input[name="payment_method"]',
b = a + ':checked',
c = '#billing_phone_field'; // The checkout field <p> container selector
// Function that shows or hide checkout fields
function showHide( selector = '', action = 'show' ){
if( action == 'show' )
$(selector).show( 200, function(){
$(this).addClass("validate-required");
});
else
$(selector).hide( 200, function(){
$(this).removeClass("validate-required");
});
$(selector).removeClass("woocommerce-validated");
$(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
}
// Initialising: Hide if choosen payment method is "cheque"
if( $(b).val() !== 'cheque' )
showHide( c, 'hide' );
else
showHide( c );
// Live event (When payment method is changed): Show or Hide based on "cheque"
$( 'form.checkout' ).on( 'change', a, function() {
if( $(b).val() !== 'cheque' )
showHide( c, 'hide' );
else
showHide( c );
});
});
</script>
<?php
endif;
}
问题是,当我 Select cheque以外的其他支付网关时,即使电话字段被隐藏,它仍然被验证并显示错误(计费电话是必填字段)-and this field needs to be filled, I don't want this!