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.

Woo Custom Empty Price – Add your own call to action when your product has no price

Selling ‘offline’ products with WooCommerce

In place of a price, I added a call to action box.

One of the e-commerce websites I look after sells a lot of heavy-duty equipment that requires installation by a specialist. Because I have no price set on some of the products, I need to show a call to action instead. This is purely for the purpose of generating leads.

Standard WooCommerce behaviour for a product with no price is to hide the add to cart button. Because of this, I had to build a plugin to add in a call-to-action box.

Continue reading “Woo Custom Empty Price – Add your own call to action when your product has no price”

WP Dev Flag – How to tell your WordPress Dev from your Production

Image courtesy of localbyflywheel.com

Ever since I managed to switch all my websites to WordPress, I’ve greatly simplified my local development setup. I don’t need multiple vagrant boxes or docker machines anymore, so for the last 12 months or so, I’ve been running all my local WordPress sites on Local by Flywheel.

Continue reading “WP Dev Flag – How to tell your WordPress Dev from your Production”

How To Customise Your WordPress Author URL – The super-easy way.

A friend of mine, Mike ( who blogs about old cameras amongst other things over at https://www.sgtberbatov.com/ ) came to me with an annoying niggle about his WordPress blog.

He showed me that his author pages had been indexed by Google, and if he did a site:sgtberbatov.com, it was listing all his author profiles in the search engine results, along with his admin username. I had a think about this and after a quick scan on WordPress.org, I realised there was no plugin available that allows you to hide your author URLs. The closest I found was something that allows you to edit your author URL slug. So…

Continue reading “How To Customise Your WordPress Author URL – The super-easy way.”