Help Wordpress

Blog about WordPress, plugins, themplates and more…
MENU
Home » Tutorials » Block the Delivery by city in WooCommerce.

Block the Delivery by city in WooCommerce.

block-the-delivery-by-city-in-woocommerce
Block the Delivery by city in WooCommerce.
5 (100%) 2 votes

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 limit the list of the US states, 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.

Posted 1 year ago
Category: Tutorials
Tags: , ,

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

Related posts:

simple-rating-bar-via-stars

Simple rating bar via stars

Simple rating bar via stars I like simple solutions, because simple solution means reliability solution. For one of the projects I needed the rating bar consisting of stars. [...]
how-pack-and-unpack-archives-via-ssh

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 [...]
upwork-fake-1

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, [...]

No Comments

No comments yet.

RSS feed for comments on this post.

Leave a comment


Last Polls: