Make Woocommerce checkout state field required

我们两清 提交于 2019-12-11 02:31:03

问题


So this problem may have a easy solution, but I'm stuck for the moment. After the last update (Woocommerce 3.3.5) I have a problem with the state field on the checkout page, because it is not mandatory and people just skip it. I really need this thing to be mandatory, because I have connected my website to the delivery company server through and API to send the order info directly to them.

I tried adding this to my functions.php and the thing is that when I go to the checkout page, the field has an asterisk, but for like one second.

add_filter( 'woocommerce_billing_fields', 'woo_filter_state_billing', 
10, 1 );
add_filter( 'woocommerce_shipping_fields', 
'woo_filter_state_shipping', 10, 1 );

function woo_filter_state_billing( $address_fields ) { 
$address_fields['billing_state']['required'] = true;
return $address_fields;
}
function woo_filter_state_shipping( $address_fields ) { 
$address_fields['shipping_state']['required'] = true;
return $address_fields;
}

Any help is appreciated. Thanks!


回答1:


By default in Last woocommerce version 3.3.5, state field is required… So in your case something is making that field "not" required.

You can try this (work for billing and shipping fields at the same time):

add_filter( 'woocommerce_default_address_fields' , 'make_state_field_required', 90, 1 );
function make_state_field_required( $address_fields ) {
     $address_fields['state']['required'] = true;

     return $address_fields;
}

Code goes in function.php file of your active child theme (or active theme). It could works.




回答2:


This worked better for me as of January 2019

add_filter( 'woocommerce_billing_fields', 'woo_filter_state_billing', 10, 1 );
add_filter( 'woocommerce_shipping_fields', 'woo_filter_state_shipping', 10, 1 );
function woo_filter_state_billing( $address_fields ) {
  $address_fields['billing_state']['required'] = true;
    return $address_fields;
}
function woo_filter_state_shipping( $address_fields ) {
    $address_fields['shipping_state']['required'] = true;
    return $address_fields;
}



回答3:


Maybe you mean "country"? I had that problem, being that field inexplicably optional. In that case, the code would be:

add_filter( 'woocommerce_billing_fields' , 'make_country_field_required', 90, 1 );
function make_country_field_required( $address_fields ) {
     $address_fields['billing_country']['required'] = true;

     return $address_fields;
}


来源:https://stackoverflow.com/questions/50485012/make-woocommerce-checkout-state-field-required

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!