WooCommerce Registration Form Postcode Field Validator

WooCommerce register form postcode field


There are no pre-requisites in order for you to be able to implement this solution.

How To Implement This Solution?

Simply copy the pre-coded solution to your active theme’s functions.php or preferably the Code Snippets Plugin.

About this Solution

If you mange a store which does not allow for local pickup and you only deliver to specific postcodes, you may wish to save customers the hassle and aggravation of going through the entire checkout process necessarily, simply by asking the user for their postcode when they register. This quick guide adds a custom postcode field to the register form, then compares it against a list of postcodes which you define. If the first two characters of the postcode entered match the list, the user is able to register. If the first two characters of the entered postcode do not match any from the list, an error message will appear and the user is prevented from being able to register.

* Snippet Name:		WooCommerce registration form postcode field validator
* Snippet Author:	ecommercehints.com

// Create the field and display it on the registation form
add_action('woocommerce_register_form', 'ecommercehints_registraion_form_postcode_field');
function ecommercehints_registraion_form_postcode_field() {
         'type'        => 'text',
         'required'    => true,
         'label'       => 'Your Postcode',
         'placeholder' => 'Postcode',
         'description' => 'To check if we deliver to you'
      (isset($_POST['billing_postcode']) ? $_POST['billing_postcode'] : '')

// Show an error message if the postcode is empty or not accepted
add_action('woocommerce_register_post', 'ecommercehints_postcode_field_validation', 10, 3);
function ecommercehints_postcode_field_validation($username, $email, $errors) {
   if (empty($_POST['billing_postcode'])) {
      $errors->add('billing_postcode_error', 'Your postcode is a required field');
   $postcode = substr($_POST['billing_postcode'],0,2);
   $postcodes = array(
      'DD', 'DA', 'CW', '', ' ', // The list of postcodes accepted
   if (!in_array(strtoupper($postcode), $postcodes)) {
   $errors->add('billing_postcode_error', 'Sorry, you cannot register as we don\'t deliver to you');  

// Save the postcode as user meta
add_action('woocommerce_created_customer', 'ecommercehints_save_postcode_field');
function ecommercehints_save_postcode_field($customer_id) {
   if (isset($_POST['billing_postcode'])) {
      update_user_meta($customer_id, 'billing_postcode', wc_clean($_POST['billing_postcode']));
WooCommerce Conversion Rate Optimisation (CRO) eBook
100 WooCommerce Conversion Rate Optimisation Tips

4 Responses

  1. Sorry, if you could help me.
    I did not understand what you means with: “This quick guide adds a custom postcode field to the register form”.

    What is this “register form”? Is there a way to use just the zip code to open the store page (no register name or email)?

    1. Hey Fábio,

      The registration form is where users register for an account on your website.

      The easiest way we envisage this working is create a popup using Elementor Pro ($49/Year) and place the registration form in this popup. Then, in the popup settings, force the popup to show until the user has signed in. This may impact SEO though.

      In the meantime, you could go to Dashboard > WooCommerce > Settings > Accounts & Privacy > Guest Checkout and make sure “Allow customers to place orders without an account” isn’t ticked and make sure “Allow customers to log into an existing account during checkout” is ticked. This way, users cannot checkout without first creating and logging into their account.

      Feel free to contact us with exact requirements e.g a popup needed or an example you have seen so we can properly scope out the work and give you a quote.

      Hope this helps!

  2. Hello, The snippet works so far. But I want to enter numbers there and not letters. It works with letters but not with numbers. How can I change this?

    1. Hello Rene, lines 28 – 34 will need amending. At the moment, the code checks the entered postcode matches the first two characters defined list on line 30. If you can’t get your head around it, drop us a message here and we’ll send you a quote to do it for you: https://ecommercehints.com/contact

Leave a Reply

If you are going to write code in the comments, please wrap it between code tags.

Your email address will not be published. Required fields are marked *

Other Recent Guides

Subscribe To Emails

Get exclusive WooCommerce tips that I only share with email subscribers

Join hundreds of other subscribers