My Account

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:

// 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']));

100 WooCommerce Conversion Rate Optimisation Tips

This field is for validation purposes and should be left unchanged.

Let’s collaborate!

Need to outsource WordPress development?

Join forces with UnlimitedWP for an outsourced white label web development service you can truly rely on.

First Timer Here?

Sign up to receive 20% off on your first month with us.