An annoying thing for sellers based in Ireland (for example), is that we still don’t use post codes (they recently introduced them, but nobody’s using them). So, in today’s task, I want to show you how to disable the “REQUIRED” feature of the postcode on the checkout page.
We already saw how to completely remove the PostCode/ZIP field… but this time we wish to leave it on the checkout, and just avoid a validation error if we don’t enter anything.
PHP Snippet (Alternative #1): Disable Postcode/ZIP Validation @ WooCommerce Checkout
/** * @snippet Disable Postcode/ZIP Validation @ WooCommerce Checkout * @how-to Get CustomizeWoo.com FREE * @sourcecode https://businessbloomer.com/?p=20203 * @author Rodolfo Melogli * @testedwith WooCommerce 3.5.3 * @community https://businessbloomer.com/club/ */ add_filter( 'woocommerce_default_address_fields' , 'bbloomer_override_postcode_validation' ); function bbloomer_override_postcode_validation( $address_fields ) { $address_fields['postcode']['required'] = false; return $address_fields; }
PHP Snippet (Alternative #2): Disable Postcode/ZIP Validation @ WooCommerce Checkout
/** * @snippet Disable Postcode/ZIP Validation @ WooCommerce Checkout * @how-to Get CustomizeWoo.com FREE * @sourcecode https://businessbloomer.com/?p=20203 * @author Rodolfo Melogli * @testedwith WooCommerce 3.5.3 * @community https://businessbloomer.com/club/ */ add_filter( 'woocommerce_checkout_fields' , 'bbloomer_alternative_override_postcode_validation' ); function bbloomer_alternative_override_postcode_validation( $fields ) { $fields['billing']['billing_postcode']['required'] = false; $fields['shipping']['shipping_postcode']['required'] = false; return $fields; }
Official Documentation
https://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
Not working in woocommcerce 9. using latest WordPress block editor
You can’t do any customization / custom validation on the Woo Checkout Block as of now…
Hi Rodolfo. Love your code snippets!
However in this case, you are making the field not mandatory – it becomes optional with your code. I guess by definition, if it’s optional then there’s no validation.
But if you want to keep the field as mandatory but remove the validation of the post code (so you can add any text in there) then I suggest the following:
Nice!
Greetings Rodolfo,
First off, great work with all the information and snippets you provide. They’ve proven to be very usefull in the past allready!
I’ve tried your snippet to disable address validation but can’t seem to get it to work. Our client sells to customers in France and it’s custom there to write the housenumber in front of the street name, eg. “1 Place general”. Woocommerce triggers a validation error on this. using your code provided, I’ve used the snippet below but this doesn’t seem to work. Do you have any further advice for this perhaps?
Hey Nikolaj, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi!
I have tried both of these code one by one and twist, but no changes on invalid and required. My Woocommerce version is 5.0.2 .Can you help me to solve this. Please!
Hey Nur, thanks so much for your comment! I just retested this on the latest version of WooCommerce and it still works. Unfortunately this looks like custom troubleshooting work and I cannot help here via the blog comments. Thanks a lot for your understanding! ~R
Hi,
Trying to do this but getting the following error;
Your PHP code changes were rolled back due to an error on line 1131 of file wp-content/themes/oceanwp/functions.php. Please fix and try saving again.
syntax error, unexpected ‘add_filter’ (T_STRING), expecting function (T_FUNCTION)
Hey James, thanks for your comment! I checked again and it seems there is nothing wrong with my snippets – maybe you lost a comma or something else while copying/pasting?
Thanks,
The first actually works for me.
But the second . . . it didn’t
Inasmuch as the first code solved the problem, I was so excited.
Once again thanks.
Great 🙂
When I use either of these alternatives, it works great. However, I use calculated shipping as I ship all over the world and the shipping no longer calculates at all. Do I have to provide flat rate or free shipping for this to work properly? Does Woocommerce depend on the zip code field to use calculated shipping?
Hey Sara, thanks so much for your comment! WooCommerce might depend on zip code if the shipping methods do in the settings. Other than that, you probably have some other problem (incompatible theme, error in the PHP, etc)
Try completing all fields that are required. Usually is zip is optional woocommerece calculates according to another field.
I’ve just tried this and added both snippets into my theme’s functions.php file worked like a charm thank you.
Excellent 🙂
Yes, I’m afraid it doesn’t remove the validation. It does remove the required astrick, however. Still on the hunt for the validation part of it.
Ok – I added a second alternative, let me know 🙂
it doesnt remove post code input, but it disable required field. Thanks for solution.
🙂
I wish to totally remove the e-mail requirement and the compulsory option that is on the checkout page.I am shown that this requirement is basic for the purpose of validation but I dont need it because my site uses M-pesa mostly for purchases and the service is virtual so this requirement puts off my customers. the only requirements i need on the check out are name, country and phone number,pls assist.
Hey Samuel, thanks for your comment! Unfortunately the email is the only field you can’t really remove 🙂
Hi Rodolfo, I tried your snippet in my child theme functions.php file and it removed the asterisk for a required field, but I still get the validation error. I have the latest woocommerce version 2.6.14. Any ideas?
Hey Jason, thanks for your comment! I tested the snippet on the same version and I didn’t get any error. What country did you select in the checkout?
You have saved the day! Cheers for the snippet 🙂
Presume this could disable required for State / County too?
Glad to hear that Steve, thanks for your comment! And yes, you can do the exact same with other fields e.g.:
Thanks for the article, it would be better if you would update the snippet that only selected countries will make the postcode validation false. For example:
When you select ireland, postcode validation is not required. But if you select united kingdom, postcode validation is required.
What do you think??
Thank you Jake, that’s a good idea! However, the fix you suggested is not simple at all as fields cannot be removed “dynamically” once the country is selected. It’s not a PHP-only job 🙂 I’ll think about it and see if I can come up with something hopefully. Cheers!