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!
PHP Snippet: Add a Custom Country @ WooCommerce Admin / Checkout
/**
* @snippet Custom Country @ WooCommerce Checkout
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @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;
}
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
Hiya Breda! Do you get that error on the Checkout page?
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
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?
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’.”
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?
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.
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/