WooCommerce has a postcode validator built in which is used to find appropriate shipping methods on the checkout. In case you are going to extend some shipping methods or build something else with postcodes, you will learn here how you can use that for your own solution.

The WooCommerce function which you can (and should) use to validate postcodes is wc_postcode_location_matcher.

Here is the full code of that function:

Here are the parameters that you can use:

  • $postcode – The postcode you want to validate,
  • $objects – This is an array of objects (each object can be one postcode or a range of them),
  • $object_id_key – This is the attribute in the objects that will use to map the validated postcodes,
  • $object_compare_key – This is the attribute in the objects that is used to check against the $postcode,
  • $country – if provided, it will get specific wildcard postcodes. Optional.

So, here is an example to understand this.

$postcode: 51000.

$objects:

$object = new stdClass();
$object->id = 'zone_croatia';
$object->postcode = '51000';

$objects = array( $object );

So now, we have a $postcode and $objects. Now, inside of the function we would use 'id' as $object_id_key to map the found matching postcodes into a new array that the function wc_postcode_location_matcher returns.

And to compare the postcode we want to validate, we will use 'postcode' as $object_compare_key.

How WooCommerce uses Postcode validator?

We will now see the code where WooCommerce uses information from their shipping zones to match with the postcode.

In this code, they get all the zone information that is a postcode. Then they try to find any zones that match the provided postcode.

That is done by providing the ‘zone_id’ as the object id and the ‘location_code’ as the object compare key. Why? Because the results retrieved from the SQL above will provide you will an array of object with those attributes.

Then, they try to find any zones that do not match that postcode and continue building a query to exclude any shipping methods that do not match that postcode.

Extending a Shipping Method with Postcodes

On a recent project, a client has used a shipping method to create a table rate and define multiple rates as shipping methods.

The problem here is that this shipping method was used globally you could not define multiple table rates to use with shipping zones. So this shipping method could not be limited only to a few postcodes.

Since this plugin could not be found anymore on the internet except here: http://hookr.io/plugins/woocommerce-table-rates/#index=p, I had to build something myself. I’ll write the code here so you can use it for any shipping method (for adding the field) and also the code to validate methods.

Let’s now add the field for entering postcodes.

To make this postcodes matched with the current shipping postcode, we need to filter into the shipping method.

So, what is happening here?

  1. We check if it’s available. If not, we return the given $available,
  2. We get the saved postcodes. If there are none, we return the given $available,
  3. We prepare the postcodes so that all are uppercase, cleaned and we make an array out of them by making each new line, a new array item,
  4. Then we get the shipping postcode and country and build the objects for the saved postcodes,
  5. We try to find any matching postcodes,
  6. If we don’t have any, we make this method unavailable, otherwise we make it available.

Conclusion

The WooCommerce contains a lot of useful functions that can be used to extend your experience and also the experience of your customers. With the WooCommerce postcode validator, you can make various custom solutions for your store.

Some of them can be:

  • Validate a postcode when building their own profile/account,
  • Create a postcode deliver check form so people can immediately check if you deliver to them.

Have you done something custom using some of the core WooCommerce functions such as this one? Let others know in the comments below.

Sponsored By

Become a Sponsor

Posted by Igor Benic

Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at https://leanpub.com/u/igorbenic.

Leave a reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.