Categories
WooCommerce Extensions

Cities Shipping Zones for WooCommerce

Which uses shop owners make with the customer city?

  • Set shipping rates per cities
  • Sales stats by city (Dashbaord => WooCommerce => Reports => Sales by city)
  • Display cities shipping calculator with the shortcode [csz_cities]
  • Sell certain products per city (with plugin that limits by state)
  • Ship certain products per city (associating the products to a class without price in shipping method which doesn’t have based price)
  • Apply taxes per city (use the city code in the state field in the tax settings)
  • Enable payment / shipping methods per city (with conditional payment / shipping plugin)
  • Shipping for cities by the day of the week (with order delivery date plugin that has shipping methods based option)
  • Display the cities in external checkout page (retreive the states via WooCommerce REST API)
Options

What is the State Autofill option?
When enabled, the state will be auto filled by the selected city for countries that have states in their cities list. You may set the countries the option will be applied on with the csz_populate_state filter.

What is the Filters option?
When enabled, a state filter will be displayed for the store country in the checkout and the shipping calculator. the state will not be inserted into the order unless the ‘State Autofill’ option is enabled.

Bulk Edit Tool

What is the required format?
Semicolon (;) between each city. For example: Wien; Rust; Steinbrunn

How to modify cities in list to the required format?
Replace [\r\n]+ (regex) with ; in Notepad++.

How to enter all the locations within certain distance from a city with distances list?
Enter exclamation mark (!) followed by the city code, if necessary add minium and maximum distance seperated by semicolon (;).

Integrations

Why in the integrated software the city appear twice or the city code is present?
You will have to remove the sending of the state field (billing/shipping, orders/customers) via its plugin’s code for the countries you apply the plugin on.

Why in my integrated software the city field is empty or the payment failed?
You will have to modify its way of retreiving the city field (billing/shipping) from the order for the countries you apply the plugin on- from: $_POST['billing_city'] into $order->get_billing_city(). As alternative, add to the woocommerce_checkout_create_order action a command that copies $order->get_billing_city() into $_POST['billing_city'].

Why the state code and not the state name appears in the order details in the first order email confirmation and in the my account section while using the State Autofill option on countries with WooCommerce built-in states list?
You may use the woocommerce_formatted_address_replacements filter to fix it.

How to prevent the loading of the cities in WCFM plugin pages?

add_filter( 'csz_enable_cities', 'csz_disable_cities_wcfm' );
add_filter( 'woocommerce_states', 'csz_load_states_wcfm', 9999 );

function csz_disable_cities_wcfm( $cities_enabled ) {
	return isset( $_GET['store-setup'] ) && 'yes' === $_GET['store-setup'] || false !== strpos( $_SERVER['REQUEST_URI'], 'store-manager' ) ? false : $cities_enabled;
}

function csz_load_states_wcfm( $states ) {
	if ( isset( $_GET['store-setup'] ) && 'yes' === $_GET['store-setup'] || false !== strpos( $_SERVER['REQUEST_URI'], 'store-manager' ) ) {
		foreach ( get_option( 'wc_csz_countries_codes' ) as $country_code ) {
			include( WP_PLUGIN_DIR . '/cities-shipping-zones-for-woocommerce/i18n/cities/' . $country_code . '.php' );
			if ( $country_states ) {
				$states[ $country_code ] = $country_states;
			}
		}
	}
	return $states;
}

How to integrate with Food Online Premium plugin?
Prevent the customer state from beeing loaded in config_default_customer_address function and add to line 836 in class-fdoe-del.php file (update the mismatch cities names):

if ( is_plugin_active( 'cities-shipping-zones-for-woocommerce/cities-shipping-zones-for-woocommerce.php' ) && in_array( $country, get_option( 'wc_csz_countries_codes' ) ) ) {
	$cities = [
		'Talin' => 'Tallinn',
	];
	if ( isset( $cities[ $_POST['city'] ] ) ) {
		$_POST['city'] = $cities[ $_POST['city'] ];
	}
	$state = array_search( _x( wc_clean( $_POST['city'] ), "{$country}-cities", 'cities-shipping-zones-for-woocommerce' ), WC()->countries->get_states( $country ) );
	$_POST['city'] = '';
}
Cities List

Why some locations are absent?
The list of most of the countries contains the municipalities, so villages within those municipalities will not be present.

How to modify cities names?
Update countries codes, and cities codes and names (not compatible with the Filters option):

add_filter( 'csz_cities', 'csz_modify_cities' );
function csz_modify_cities( $states ) {
	$states['AT']['AT90001'] = 'Vienna';
	return $states;
}

How to remove some cities from the list?
Update country code and cities codes:

add_filter( 'csz_cities', 'csz_remove_cities' );
function csz_remove_cities( $states ) {
	$country_code = 'AT';
	$unnecessary_cities = [ 'AT10101', 'AT10201', 'AT10301' ];
	foreach ( $unnecessary_cities as $city ) {
		unset( $states[ $country_code ][ $city ] );
	}
	return $states;
}

How to keep only specific cities in the list?
Update country code and cities codes:

add_filter( 'csz_cities', 'csz_set_cities' );
function csz_set_cities( $states ) {
	$country = 'AT';
	$cities = [ 'AT10101', 'AT10201', 'AT10301' ];
	$new_states = [];
	foreach ( $cities as $city ) {
		if ( isset( $states[ $country ][ $city ] ) ) {
			$new_states[ $city ] = $states[ $country ][ $city ];
		}
	}
	$states[ $country ] = $new_states;
	return $states;
}

How to remove from the list all the cities of certain states?
Update states codes:

add_filter( 'csz_states', 'csz_remove_states_cities' );
function csz_remove_states_cities( $states ) {
	$unnecessary_states = [ 'EE001', 'EE004', 'EE006' ];
	foreach ( $unnecessary_states as $state_code ) {
		if ( isset( $states[ $state_code ] ) ) {
			unset( $states[ $state_code ] );
		}
	}
	return $states;
}

How to create shipping zones for quarters inside a city?
Create shipping zone with that city and restrict to the relevant postcodes, or (not compatible with the State Autofill / Filters options and 3-party integrations) split the city into areas (update country code and city code and areas names, if the store is located inside this city update the store location in WooCommerce settings):

add_filter( 'csz_cities', 'csz_split_cities' );
function csz_split_cities( $states ) {
	$country_code = 'AT';
	$city_code = 'AT90001';
	$sub_cities = [ __( 'Northern', 'woocommerce' ), __( 'Southern', 'woocommerce' ) ];
	$count = 101;
	foreach ( $sub_cities as $sub_city ) {
		$states[ $country_code ][ $city_code . $count ] = $states[ $country_code ][ $city_code ] . ' - ' . $sub_city;
		$count++;
	}
	unset( $states[ $country_code ][ $city_code ] );
	return $states;
}

How to let the customer to select a city for the order which is not displayed in the list?
Temporary don’t apply the plugin on any country and remove all the saved states values of your current customers from those countries to prevent loading the state in the checkout city field:

add_filter( 'csz_enable_custom_city', '__return_true' );
Shortcode

How to display the shipping calculator shortocde message permanently?
Not compatible with multinational stores:

add_filter( 'wp_footer', 'csz_custom_shipping_message' );
function csz_custom_shipping_message() {
	?>
		<script type="text/javascript">
		jQuery( function( $ ) {
			$( document ).on( 'zone_matched', function( event, response ) {
				$( '#shipping_state-description' ).remove();
				if ( ! $( '#shipping_message' ).length ) {
					$( '#shipping_state_field' ).append( '<span id="shipping_message">response</span>' );
				}
				$( '#shipping_message' ).text( response );
				$( '#shipping_message' ).css( { 'color':'black', 'background-color':'yellow' } );
			} );
		} );
		</script>
	<?php
}
Distance Fee

How to apply distance fee?
Given a (domestic) city’s distances list, it will be possible to add distance based fee for the shipping rate or product price (for example- to sell moving services).

Why the tax is not calculated on the distance fee?
Enter in the shipping method cost greater than zero.

Where the configured product distance fee will be displayed?
The product the fee will be added to its price and will be displayed in the subtotal of the mini cart widget.

How I shall organize the distances list for some city?
Use as index the locations codes or names (as they appear in the dashboard and update $key_format), verify to set distance to the city itself, the distances should be greater than 0.

WooCommerce REST API

Use the class:

add_filter( 'woocommerce_rest_api_get_rest_namespaces', 'wc_states_api' );
function wc_states_api( $controllers ) {
	$controllers['wc/v3']['data-states'] = 'WC_REST_Data_States_Controller';
	return $controllers;
}

class WC_REST_Data_States_Controller extends WC_REST_Data_Controller {
	protected $namespace = 'wc/v3';
	protected $rest_base = 'data/states';
	function register_routes() {
		register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<state>[\w-]+)/zone', [
			'methods'		=> 'GET',
			'callback'		=> [ $this, 'get_zone' ],
			'args'			=> [ 'state' => [ 'description' => __( 'State / County', 'woocommerce' ), 'type' => 'string' ] ],
			'permission_callback'	=> [ $this, 'get_items_permissions_check' ],
		] );
		register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<state>[\w-]+)/state', [
			'methods'		=> 'GET',
			'callback'		=> [ $this, 'get_state' ],
			'args'			=> [ 'state' => [ 'description' => __( 'State / County', 'woocommerce' ), 'type' => 'string' ] ],
			'permission_callback'	=> [ $this, 'get_items_permissions_check' ],
		] );
	}
	function get_zone( $data ) {
		return [ 'id' => wc_get_shipping_zone( [ 'destination' => [ 'country' => substr( $data['state'], 0, 2 ), 'state' => $data['state'], 'postcode' => '' ] ] )->get_zone_id() ];
	}
	function get_state( $data ) {
		foreach ( get_option( 'wc_csz_countries_codes' ) as $country_code ) {
			$country_cities = '';
			$country_states = '';
			include( WP_PLUGIN_DIR . '/cities-shipping-zones-for-woocommerce/i18n/cities/' . $country_code . '.php' );
			if ( isset( $country_states ) ) {
				foreach ( $country_states as $state_code => $state_name ) {
					if ( isset( $country_cities[ $state_code ][ $data['state'] ] ) ) {
						return $country_states[ $state_code ];
					}
				}
			}
		}
		return false;
	}
}

How to retrieve the shipping zone a city belong to (for the countries the plugin apply on)?

/wp-json/wc/v3/data/states/<city_code>/zone

How to retrieve the state name a city belong to (for the countries the plugin apply on)?

/wp-json/wc/v3/data/states/<city_code>/state
Uninstall Plugin / Countries / Locations
  • WooCommerce Shipping Zones settings: Remove the locations (you may use the Bulk Edit tool)
  • Plugin Settings: Remove the countries
  • WooCommerce Settings: Update the store location
  • Users: convert/erase the state field values of users that were created/updated while the plugin was applied on the countries they belong to
  • Plugins: Deactivate and delete the plugin

48 replies on “Cities Shipping Zones for WooCommerce”

Hi,

I was trying to use your “Cities Shipping Zones for WooCommerce” plugin. However, the country I am trying to use this plugin is not supported. I would to request to add “United Arab Emirates” to the supported countries.

Thank you in advance.

Raditya, Indonesia is supported but the address format in Indonesia contains states, so if you use the plugin you will not able to integrate your website with online payment or shipping software.

That’s a bummer! Guess I’ll have to add them by zip codes, which is a lot of work to do. Thanks for the fast reply and wish you have a great day!

Hi, in the latest plugin update the ‘autofill state’ option was added, when enabled the integration with online payment and shipping software should function as usual.

Dear, please help on following
1. could you please add Myanmar as a supported country
2. how to add different cities in Myanmar, where shipping zone applies as customer enters the city at check out page.

Thank you

Excellent complement!
Congratulations

I have seen “How to modify cities names?”, it could be a solution but I don’t know what the city codes of my country / state are

Can I just add a city?
Where can I get the country / city codes to modify them?

Thanks!

Hi,

I was trying to use your “Cities Shipping Zones for WooCommerce” plugin. However, the country I am trying to use this plugin is not supported. I would to request to add “Morocco” to the supported countries.

Thank you in advance.

Hi Krishneel, Fiji is better organized by its provinces so no need to use the plugin just add this to your theme’s functions.php file:
add_filter( 'woocommerce_states', 'custom_woocommerce_states' );
function custom_woocommerce_states( $states ) {
$states['FJ'] = [
'FJ01' => 'Ba',
'FJ02' => 'Bua',
'FJ03' => 'Cakaudrove',
'FJ04' => 'Kadavu',
'FJ05' => 'Lau',
'FJ06' => 'Lomaiviti',
'FJ07' => 'Macuata',
'FJ08' => 'Nadroga and Navosa',
'FJ09' => 'Naitasiri',
'FJ10' => 'Namosi',
'FJ11' => 'Ra',
'FJ12' => 'Rewa',
'FJ13' => 'Serua',
'FJ14' => 'Tailevu',
];
return $states;
}

Hi, make sure Sri Lanka was selected in the plugin settings ‘Country / Region’ field (‘The countries to apply the plugin on’), in WooCommerce Shipping Settings click edit on the first shipping zone and insert some cities of Sri Lanka in its ‘Zone regions’ field.

Hi Obinna, After you have selected Nigeria via the plugin settings, go to shipping zone edit screen (Dashboard => WooCommerce => Settings => Shipping) and select the cities in the Zone regions field.

I’m having some issues while integrating with cybersource, and I need to be able to set the “state” to the same as “country” (as cybersoruce support indicated) so, can you please help me to achieve this?

Hi, can you provide more details? the state field is filled only if the ‘State Autofill’ option is enbaled, do they ask that for the US or for other countries? Does it work when the plugin is disabled?

There’s no file named as states.php in plugins folder. I want to set shipping price 200Rs for Karachi a city in Pakistan and 280Rs for other cities of Pakistan. Is it possible to do with this plugin please let me know thanks!

Regards
Riaz Ejaz

Dear Riaz,
Select Pakistan via the plugin settings and press ‘Save Changes’, then create 1 shipping zone with flat rate shipping method (200Rs) and choose Karachi in the Zone regions field, and another shipping zone with flat rate shipping method (280Rs) and choose Pakistan in the Zone regions field, make sure the Karachi shipping zone is above the Pakistan shipping zone.

Hi there,

I’m a wordpress developer, working actually on a multiple pickup and shipping location e-shop. I have some questions about this awesome Cities Shipping Zone for WooCommerce :

1. I have removed unnecessary cities in the French city list. In order to reduce load time.
2. On the checkout and summary page our customers can only choose cities from the drop-down list, but we also have some customers from outside the zone come to us to pick up the order.
3. So could you add for example « 0 – other cities » on the top of the drop-down list and when the customer choose this option the dropdown transform to text input for customers to enter the city name manually.

I think this option could be done with a little more conditional logic (if…else), and lots of users need to this in order to make invoice with the right city name of users.

Thank you for the reply and help.

Hi Jose, please try to add this into your theme’s functions.php file:
add_filter( 'woocommerce_get_country_locale', 'csz_modify_city_label', 9999 );
function csz_modify_city_label( $locale ) {
$locale['PE']['state']['label'] = __( 'Distrito' );
return $locale;
}

Leave a Reply

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