Wordpress 如果选择“货到付款”,则隐藏帐单地址
在WooCommerce中,我希望在选择货到付款时能够隐藏帐单地址字段,因为不需要填写地址。但如果客户选择信用卡,则应显示账单地址Wordpress 如果选择“货到付款”,则隐藏帐单地址,wordpress,woocommerce,Wordpress,Woocommerce,在WooCommerce中,我希望在选择货到付款时能够隐藏帐单地址字段,因为不需要填写地址。但如果客户选择信用卡,则应显示账单地址 我真的不知道从哪里开始,所以我没有任何代码。首先,账单地址是woo commerce的必填字段,因此,即使您将其隐藏,也不允许您结账。因此,首先需要使其成为“非必需的”,可以使用钩子来完成 然后可以运行查询 jQuery(function(){ jQuery( 'body' ) .on( 'updated_checkout', function()
我真的不知道从哪里开始,所以我没有任何代码。首先,账单地址是woo commerce的必填字段,因此,即使您将其隐藏,也不允许您结账。因此,首先需要使其成为“非必需的”,可以使用钩子来完成 然后可以运行查询
jQuery(function(){
jQuery( 'body' )
.on( 'updated_checkout', function() {
usingGateway();
jQuery('input[name="payment_method"]').change(function(){
console.log("payment method changed");
usingGateway();
});
});
});
function usingGateway(){
console.log(jQuery("input[name='payment_method']:checked").val());
if(jQuery('form[name="checkout"] input[name="payment_method"]:checked').val() == 'COD'){
$('.woocommerce-billing-fields').hide();
//Etc etc
}
}
我能够对上面的代码进行一些扩展
// Make Billing Address not required
add_filter( 'woocommerce_default_address_fields' , 'filter_default_address_fields', 20, 1 );
function filter_default_address_fields( $address_fields ) {
// Only on checkout page
if( ! is_checkout() ) return $address_fields;
// All field keys in this array
$key_fields = array('country','company','address_1','address_2','city','state','postcode');
// Loop through each address fields (billing and shipping)
foreach( $key_fields as $key_field )
$address_fields[$key_field]['required'] = false;
return $address_fields;
}
如果是COD,则隐藏账单地址;如果是信用卡,则显示账单地址
jQuery(function(){
jQuery( 'body' )
.on( 'updated_checkout', function() {
usingGateway();
jQuery('input[name="payment_method"]').change(function(){
console.log("payment method changed");
usingGateway();
});
});
});
function usingGateway(){
console.log(jQuery("input[name='payment_method']:checked").val());
if(jQuery('form[name="checkout"] input[name="payment_method"]:checked').val() == 'cod'){
jQuery('.woocommerce-billing-fields #billing_company_field, .woocommerce-billing-fields #billing_country_field, .woocommerce-billing-fields #billing_address_1_field, .woocommerce-billing-fields #billing_address_2_field, .woocommerce-billing-fields #billing_city_field, .woocommerce-billing-fields #billing_state_field, .woocommerce-billing-fields #billing_postcode_field, .woocommerce-shipping-fields').hide();
//Etc etc
} else {
jQuery('.woocommerce-billing-fields #billing_company_field, .woocommerce-billing-fields #billing_country_field, .woocommerce-billing-fields #billing_address_1_field, .woocommerce-billing-fields #billing_address_2_field, .woocommerce-billing-fields #billing_city_field, .woocommerce-billing-fields #billing_state_field, .woocommerce-billing-fields #billing_postcode_field, .woocommerce-shipping-fields').show();
}
}