I have a WordPress+Woo-commerce site and I want to make the state field on the checkout form optional based on the selected country. For example I want my customers to add state field only if they belong to one of U.S.A. states.
How do I do this?
I have a WordPress+Woo-commerce site and I want to make the state field on the checkout form optional based on the selected country. For example I want my customers to add state field only if they belong to one of U.S.A. states.
How do I do this?
Comments are closed.
In case someone stumbles on this, Woocommerce changed the name of the state fields. Below is the updated solution:
Try this (in your functions.php)
This will make the ‘state’ field optional when the visitor is not from USA.
Hope this helps.
UPDATE
This is code that worked for me. I am posting this answer without editing @zipkundan answer because I don’t see any mistakes in his answer but somehow it was not working for me. That may be because of WooCommerce version or may have another reason!! I recommend to try both answers if someone have similar requirements.
I had trouble getting the other answers to work until I realized there’s also the
woocommerce_get_country_locale
hook from WC_Countries. Which sets state required based on the chosen country through javascript in the frontend.