Restrict Shipping by Postcode in WooCommerce

In this tutorial, I will show you three methods of how you can configure ZIP/postcode-based shipping in WordPress (or, to be more specific, in your WooCommerce store).

  • The first way involves creating a custom shipping zone.
  • The second way allows you to leave your WooCommerce store shipping settings untouched, but at the same time to enable or disable specific shipping methods by a postcode(s) with the help of a custom plugin.
  • The third approach will be a programmatic one.

Now, let’s dive into it.

Method 1. Limiting Shipping Methods by Postcodes Using a Custom Shipping Zone

First of all, if you don’t need a more precise configuration, probably, for your situation, it will be enough to just create a new custom shipping zone and, in its settings, specify some specific postcodes.

So, in WooCommerce settings, let’s navigate to Shipping > Shipping zones, and then you can create a new zone by clicking the “Add zone” button.

Restrict shipping by postcodes in WooCommerce using a custom shipping zone
If the field for postcodes is not displayed for you, please click the “Limit to specific ZIP/postcodes” link.

It is not necessary to specify every ZIP code/postcode on a new line; you can also:

  • Provide a numeric range, for example, in our case, it would be 0101...0109 or something like this.
  • Use a wildcard, for example, if you want to cover all postcodes from 0100 to 0109, you can use the following wildcard: 010*.

As you can see inside the settings of our brand new shipping zone, we’re able to add shipping methods to it. Yes, technically, it is not restricting or limiting existing shipping methods; it is creating new ones.

Method 2. Using Simple Conditional Shipping and Payments

If, for some reason, creating a new custom shipping zone is not an option for you, then you can also take a look at my Simple Conditional Shipping and Payments plugin.

Maybe using this plugin would be a better option for you, other than creating a new custom shipping zone, because it allows you to work with already existing shipping methods.

To configure postcode-based shipping with the plugin, in the WooCommerce settings, you just need to go to the “Conditions” tab (it is the new tab created by the plugin), then to the “Shipping conditions” section, and add a new conditional rule there.

WooCommerce restrict shipping by postcode

Some notes related to the screenshot above:

  • You can not only “Disable shipping methods” but also “Enable shipping methods”. Enabling a certain shipping method means that it will be disabled by default unless all the conditions from this rule are met.
  • As a condition, I used “Billing postcode”, but “Shipping postcode” can also be selected from the dropdown list of conditions.
  • You can add multiple conditions to the same rule; in that case, all of them must be “true” for the action to be triggered. In other words, the conditions have an “AND” relation.

The plugin also allows you to set conditions for payment methods. You can check it out here.

Method 3. Restrict WooCommerce Shipping Methods by ZIP/Postcode Programmatically

Last but not least, let’s take a look at the programmatic approach.

Maybe, setting up a new custom shipping zone is not what you need, plus you don’t want to install more plugins on your website (however, my plugin doesn’t affect your website performance in any way, and it is super-simple), so maybe a programmatic method is the way to go here.

Below is a code snippet that you can use on your website to restrict a specific shipping method for an array of ZIP codes. If you don’t know how to use code snippets, here is an explanation.

/**
 * Restrict a shipping method to specific postcodes
 */
add_filter( 'woocommerce_package_rates', 'rudr_restict_shipping_by_postcodes', 25, 2 );

function rudr_restict_shipping_by_postcodes( $rates, $package ) {

	$allowed_postcodes = array(
		'0108',
		'0107',
	);
	
	$restricted_shipping_method_type = 'flat_rate';
	$restricted_shipping_method_id = 2;
	// as a result, we will have flat_rate:2
	$restricted_shipping_method = "{$restricted_shipping_method_type}:{$restricted_shipping_method_id}";

	$customer_postcode = WC()->customer->get_shipping_postcode();

	foreach( $rates as $rate_id => $rate ) {
		
		if( $rate_id !== $restricted_shipping_method ) {
			// not the shipping method we need
			continue;
		}
		
		if( ! in_array( $customer_postcode, $allowed_postcodes ) ) {
			// restict the shipping method then
			unset( $rates[ $rate_id ] );
		}

    }

    return $rates;

}

Some notes related to the code snippet above:

  • I’m using the woocommerce_package_rates filter hook, and it is the only hook we can use when we want to enable/disable shipping methods for customers.
  • $allowed_postcodes is an array where we can specify all the target postcodes. We can also go further and add wildcards and stuff, but let’s keep this snippet simple for now.
  • Finding out the correct $rate_id could be a tricky task, because it consists of two parts – a type of shipping method (in our case, flat_rate – “Flat rate”) and then a numeric ID, which you can find when you edit that shipping method in the settings.
  • We can use WC()->customer->get_shipping_postcode() or WC()->customer->get_billing_postcode() to get the current customer’s postcode.

Please leave a comment below if you have any questions.

Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 15 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X