WooCommerce: Add a New Country For Billing/Shipping

There are times when the WooCommerce countries database is simply not enough. While the WooCommerce team is usually very fast at updating its code (though, the newest country is apparently South Sudan, which became independent in 2011, the year WooCommerce launched!), you may need to DIY in certain cases.

Think of Northern Ireland for example. It’s not a “country”, however most Irish businesses would ship to Northern Ireland and not to “UK”, so having “Northern Ireland” in the Checkout page country dropdowns may help.

In this edge case study, we’ll basically take a look at how to add a custom country, how to make sure this custom country shows at checkout as a possible option (and in the shipping zones admin section), and also how to assign to it a custom list of states. You never know!

Northern Ireland is now in the list of WooCommerce Billing & Shipping countries, thanks to the snippet below!

PHP Snippet: Add a Custom Country @ WooCommerce Admin / Checkout

/**
 * @snippet       Custom Country @ WooCommerce Checkout
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 6
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_countries', 'bbloomer_add_country' );

function bbloomer_add_country( $countries ) {
	$new_country = array(
		'XI' => 'Northern Ireland',
	);
	return array_merge( $countries, $new_country );
}

add_filter( 'woocommerce_continents', 'bbloomer_add_country_to_continent' );

function bbloomer_add_country_to_continent( $continents ) {
	$continents['EU']['countries'][] = 'XI';
	return $continents;
}

add_filter( 'woocommerce_states', 'bbloomer_add_country_states' );

function bbloomer_add_country_states( $states ) {
	$states['XI'] = array(
		'AN' => 'Antrim',
		'AR' => 'Armagh',
		'DY' => 'Derry',
		'DO' => 'Down',
		'FM' => 'Fermanagh',
		'TR' => 'Tyrone',
	);
	return $states;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

10 thoughts on “WooCommerce: Add a New Country For Billing/Shipping

  1. HI,
    I don’t think your snippet for Custom country is working with the latest woocomerce, I’m using Version 8.2.1 and I’m getting the error MISSING_SHIPPING_ADDRESS The shipping address is required when `shipping_preference=SET_PROVIDED_ADDRESS`. when i use a custom country but it works fine on a standard woocommerce country. Could you have a look at your code please? Its very useful.
    Regards
    Breda

    1. Hiya Breda! Do you get that error on the Checkout page?

      1. Hi Rodolfo
        Yes on the checkout page, if you choose Northern Ireland and fill in all the required fields, click paypal and then the error shows at the top of the checkout page.
        Regards
        Breda

        1. Tried checking out with COD and got no error, so I believe this is something to do with the PayPal plugin you use. Which one is it?

          1. Hi! We have also encountered an error upon checking out. Seems like WooCommerce doesn’t acknowledge the newly added country code ‘XI’.

            “Country ‘XI’ is unknown. Try using a 2-character alphanumeric country code instead, such as ‘US’, ‘EG’, or ‘GB’.”

            1. Checked out with BACS to test and got no error. Can you tell me which payment method gave you the error – and if you try to pay with BACS do you get it?

              1. Got the error: Country ‘XI’ is unknown. Try using a 2-character alphanumeric country code instead, such as ‘US’, ‘EG’, or ‘GB’. A full list of country codes is available at https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements

                normal woocommerce checkout with Stripe payment we got the above error.

                1. I guess the solution would be to revert to GB once the order is placed, so that whatever online payment gateway (Stripe in this case), gets the correct value (XI is not supported I guess). This could help: https://www.businessbloomer.com/woocommerce-update-checkout-field-value-after-order/

                  1. Hi,
                    I’m also having that same error with Paypal and Stripe.
                    Regarding the workaround, I couldn’t even place the order so I’m unable to edit it later…
                    Any developments regarding this issue?
                    Regards,
                    Luís

                    1. Understood. Would it work if in WooCommerce the order stays as the custom one e.g. “XI” (Northern Ireland), while the passed data to the payment gateway is instead the original one (“GB” in this case)?

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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