Help Wordpress

Help 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',
'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.

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

Related posts:

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 upwork.com – the world’s largest stock exchange of freelancing. Also this post are warning about scams, hacking accounts and sending [...]
Woocommerce exclude states – free plugin

Woocommerce exclude states – free plugin

Once in a while becomes necessary to exclude states some U.S. states on the Woocommerce checkout page. The reasons may be different – for example, basically because store [...]

Leave a Reply

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