The Canary Islands

How to add a new country to WooCommerce

This is a quick post, but I wanted to share it as it recently came in very handy. I had a complaint from a customer who was trying to buy something to ship to the Canary Islands but couldn’t find it in the list of countries in WooCommerce.

But the Canary Islands aren’t a country…

Before I continue, let me point out that the Canary Islands are not technically a country, but an autonomous community of Spain. So says Wikipedia:

DHL also make their own laws….

The Canary Islands are a group of islands off the coast of Morocco. They are an autonomous community of Spain (they make their own laws). …

Wikipedia

To demonstrate this code snippet, I’ll just go along with DHL and pretend that this is a real country. As you can see here, it’s quite a hot topic: https://canaryislandsisnotspain.wordpress.com/

The code snippet

I’m not taking any credit for this code whatsoever, as I pulled it directly from the WooCommerce docs, but here it is ( ever so slightly modified ):

/** START: ADDING CANARY ISLANDS TO SHIPPING */

add_filter( 'woocommerce_countries', 'chrisjallen_add_my_country' );

function chrisjallen_add_my_country( $countries ) {
	$new_countries = array(
	'IC' => __( 'Canary Islands', 'woocommerce' ),
);

return array_merge( $countries, $new_countries );
}

add_filter( 'woocommerce_continents', 'chrisjallen_add_my_country_to_continents' );

function chrisjallen_add_my_country_to_continents( $continents ) {
	$continents['EU']['countries'][] = 'IC';
	return $continents;
}

/*END: ADDING CANARY ISLANDS TO SHIPPING */

The code uses two filters, woocommerce_countries & woocommerce_continents. This ensures that WooCommerce can correctly include this new country in all places where it might be needed by third-party plugins. For example, in a DHL shipping plugin.

You’ll notice I added the country code of IC, but you should take your country code from the standard ISO 3166 list.

Again, it’s a bad example for me to use the Canaries! The country code I used is DHL’s own special creation, just for their benefit. Since I don’t intend to use the canaries on any other plugins, it worked fine for me.

A quick test

Once you’ve added the country code to either your own plugin, or functions.php, you should be able to see the new ‘country’ in your basket country dropdown, and also in your checkout page country dropdown.

Here’s an example of what it looks like on the website in question. If you find this useful, ( like the guy in the Canary Islands did ) then tweet me or leave a comment.


Posted

in

by

Tags:

Comments

2 responses to “How to add a new country to WooCommerce”

  1. Jean-Marc Avatar
    Jean-Marc

    I just want to thank you for that!

    1. Chris Allen Avatar
      Chris Allen

      No worries at all Jean-Marc. All the best!

Leave a Reply

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