Helper Wordpress

Helper Wordpress

Wordpress can be fast. Very fast. Really :)

Block the Delivery by city in WooCommerce.

Block the Delivery by city in WooCommerce.

Some time ago I’ve wrote on the limitation of US states in WooCommerce. However, a similar task may occur not only with the states, but also with individual counties or cities – it a matter of American administrative units.

In addition to the states, the US has the incorporated/unincorporated territory (such as Puerto Rico or the US Virgin Islands). Despite the fact that the action of the US Constitution on these islands is limited or has its own Constitution, US banks and delivery services work the same, as in the US states.

For store owner to WooCommerce which means that sometimes you may need to limit the acceptance of payments and sending orders for such territory.

Earlier, I wrote on the blog, how to exclude states for Woocommerce, and now show how you can exclude certain city. Just paste the code below to your theme’s functions.php:

add_action( 'woocommerce_checkout_process', 'webamator_validate_city' );
function webamator_validate_city() {
$disableCityList = array (
'Sarasota County',
'North Point',
'Longboat Key',
$billingCity = isset( $_POST['billing_city'] ) ? trim( $_POST['billing_city'] ) : '';
$billingCity = str_replace(array('-','_'),' ',$billingCity);
$billingCity = ucwords($billingCity);
if (in_array($billingCity, $disableCityList))
wc_add_notice( __( 'Sorry, but we cannot ship to any city in Sarasota County' ), 'error' );

This example make limit for Sarasota County, North Point, Venice and Longboat Key.

Incidentally, this solution can be used not only for the US but also for any other country. And not only for the city but also for all the other fields (such as an index or address).
Here is an example of how to restrict delivery in address field (for example, you do not want to send the products if customer used the PO Box):

add_action( 'woocommerce_checkout_process', 'webamator_validate_address_1' );
function webamator_validate_address_1() {
$billingAddress1 = isset( $_POST['billing_address_1'] ) ? trim( $_POST['billing_address_1'] ) : '';
if (preg_match('/(box)/i',$billingAddress1))
wc_add_notice( __( 'Sorry, but we cannot ship if customer use PO boxes' ), 'error' );

I hope this solutions will be useful for you. If you have any questions – welcome to comments.

If the article proved helpful to you, please click on one of the buttons to share it with your friends. Thanks!

Related posts:

Good, fast, cheap. Pick two.

Good, fast, cheap. Pick two.

You’ve probably know the phrase: “Good fast cheap. Pick two”. This concept has a few different names: it’s known as the Triple Constraint, Iron Triangle but most popular [...]
How pack and unpack archives via SSH

How pack and unpack archives via SSH

It happens, that some hosting company have inconvenient hosting control panel – and work with files is no possibility, or this feature is bad implemented / this feature [...]
Fake deals on upwork

Fake deals on upwork

This post for those freelancers who work on – the world’s largest stock exchange of freelancing. Also this post are warning about scams, hacking accounts and sending [...]

15 Replies to “Block the Delivery by city in WooCommerce.”

    1. Hi. Need additional rules for $disableCityList array. Simple examle (sure, will need get order ID first):
      $order = wc_get_order($order_id);
      foreach( $order->get_items() as $item ) {
      if( has_term( $categories, 'product_cat', $item->get_product_id() ) ) {
      $disableCityList[] = 'your_specific_city';

      1. Hi Webator!
        So I am pretty much a noob here and I dont mean to bother or anything but I do have the same question/need. I own a Bike Store where I sell TREK and SPECIALIZED bikes in Mexico, recently I decided having an online store would be great for me and I am currently trying to set it up.

        However, the brands set restriction zones to us. Mostly cities, so I can sell gloves and helmets anywhere on Mexico but I cant sell bikes on certain specific cities because we are meant to respect the trading zones… This gets me to the problem:

        How can I restrict selling any product under my “specialized” or “trek” category for x y and z cities?

        As I mentioned Earlier, I am pretty much a noob here and I do get some of the coding logic but wouln’t be able to adapt the code for this purpose. I wonder if you would like to help me please?

        1. Hi Sergio! As I understand, you wish sell products for all Mexico but some products cant sell in some sities, right? In this case need check product category before $disableCityList array.

  1. I have no knowledge of coding
    Please give me a code to restrict the sale of a categories products to a specific city and tall me where i must post that code.
    for examle i want restrict product category supermarket to Citizens of Toronto Canada
    ((That means only Toronto Canadian citizens can buy supermarket category products.))

    I am waiting for your reply…

    1. It is should be work in functions.php

      add_action( ‘woocommerce_checkout_process’, ‘webamator_validate_city’ );

      function webamator_validate_city() {

      $billingCity = isset( $_POST[‘billing_city’] ) ? trim( $_POST[‘billing_city’] ) : ”;

      if ($_POST[‘billing_city’] !== ‘Toronto’)
      wc_add_notice( __( ‘Sorry, but we shipping to Toronto only’ ), ‘error’ );


  2. tank you for your help
    but i want sale product to all place Except supermarket category ;
    supermarket category jast to a specific city like Toronto

  3. Hi,
    I have same requirement.I have different kind Categories on my website.But grocery category products want to ship only one particular city.(Want to restrict other area restrict).Other categories Shipping to All cities.
    Domain –
    Country – Srilanka
    I have tried with several WooCommerce shipping pluggins but not met the requirement.
    Please guide.

Leave a Reply

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