在WooCommerce中,我希望能够在选择“货到付款”时隐藏“帐单地址”字段,因为无需填写地址。但是,如果客户选择信用卡,则应显示帐单邮寄地址。
我真的不知道从哪里开始,所以我没有任何代码。
答案 0 :(得分:0)
答案 1 :(得分:0)
首先,woo Commerce需要填写帐单地址,因此,即使您隐藏了帐单地址,它也不允许您结帐。因此,首先需要使它“不是必需的”,可以使用钩子完成
Make checkout addresses fields not required in WooCommerce
然后您可以运行查询
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
}
}
答案 2 :(得分:0)
我可以在上面的代码上扩展一点...
// 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();
}
}