为什么不让我删除地址字段?

时间:2019-08-28 17:44:48

标签: wordpress woocommerce

我有1个具有2个值的字段单选按钮:交货和提货。

如果有人选择取件,我希望删除所有不必要的字段:address1,address2,城市,邮政编码,国家/地区(也必须删除)。

如果检查了交货值,则按原样工作。



add_action('woocommerce_checkout_process', 'wps_select_checkout_field_process');
 function wps_select_checkout_field_process() {
    global $woocommerce;

 if ($_POST['delivery'] == "Ridicare")

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
unset($fields['billing']['billing_postcode']);
unset($fields['billing']['billing_country']);
unset($fields['billing']['billing_state']);
unset($fields['billing']['billing_address_1']);
unset($fields['billing']['address-line1']);
unset($fields['billing']['billing_postcode']);
unset($fields['billing']['billing_company']);


return $fields;
}

 }


验证时,我从地址字段中收到错误消息...

1 个答案:

答案 0 :(得分:0)

“ woocommerce_checkout_fields”挂钩删除了结帐页面上的字段,但您下订单后,woocomerce检查所有已提交的验证。

因此,使用它禁用邮政编码和状态验证会更容易。

add_filter('woocommerce_default_address_fields','misha_disable_address_fields_validation');

function misha_disable_address_fields_validation($ address_fields_array){

unset( $address_fields_array['state']['validate']);
unset( $address_fields_array['postcode']['validate']);
// you can also hook first_name and last_name, company, country, city, address_1 and address_2

return $address_fields_array;

}

https://rudrastyh.com/woocommerce/checkout-fields.html