
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',
'Venice',
'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.
Related posts:


Woocommerce plugins discount from helper-wp.com, save 20%
Dear customers, I am glad to inform you that all the year 2018 I give a Woocommerce plugins discount. You can save up to 20% on all plugins [...]
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 [...]
Hi,does this snippet work with the new version?
Hi, Laura
Yes, it work for WooCommerce 3.5+
Hi
Great snippet.
How to adjust it if I need to restrict product-category for specific cities ?
Thanks
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';
}
}
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?
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.
can this be adjusted for shipping address?
Hi Mike, sure. Need just to change billing_city on shipping_city
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…
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’ );
}
}
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
Hi,
I tried your code and modify the city’s name to Hebrew but it’s not working.
Any chance you can tell me why?
I want to limit the city’s that my client want to deliver to.
Thanks!
Gabriel
hm… as I know, Hebrew dont have lowercase/uppercase, right? For this reason try without “ucwords” function
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 – payless.lk
Country – Srilanka
I have tried with several WooCommerce shipping pluggins but not met the requirement.
Please guide.
Thanks
Hi, you need get product, then get product category and create additional rule for this category.