Skip to content

Instantly share code, notes, and snippets.

@helgatheviking
Created December 7, 2015 19:01
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save helgatheviking/5bd7955cbcef0eed42ae to your computer and use it in GitHub Desktop.
Save helgatheviking/5bd7955cbcef0eed42ae to your computer and use it in GitHub Desktop.
Converts the WooCommerce country codes to 3-letter ISO codes
<?php
/**
* Converts the WooCommerce country codes to 3-letter ISO codes
* https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
* @param string WooCommerce's 2 letter country code
* @return string ISO 3-letter country code
*/
function kia_convert_country_code( $country ) {
$countries = array(
'AF' => 'AFG', //Afghanistan
'AX' => 'ALA', //&#197;land Islands
'AL' => 'ALB', //Albania
'DZ' => 'DZA', //Algeria
'AS' => 'ASM', //American Samoa
'AD' => 'AND', //Andorra
'AO' => 'AGO', //Angola
'AI' => 'AIA', //Anguilla
'AQ' => 'ATA', //Antarctica
'AG' => 'ATG', //Antigua and Barbuda
'AR' => 'ARG', //Argentina
'AM' => 'ARM', //Armenia
'AW' => 'ABW', //Aruba
'AU' => 'AUS', //Australia
'AT' => 'AUT', //Austria
'AZ' => 'AZE', //Azerbaijan
'BS' => 'BHS', //Bahamas
'BH' => 'BHR', //Bahrain
'BD' => 'BGD', //Bangladesh
'BB' => 'BRB', //Barbados
'BY' => 'BLR', //Belarus
'BE' => 'BEL', //Belgium
'BZ' => 'BLZ', //Belize
'BJ' => 'BEN', //Benin
'BM' => 'BMU', //Bermuda
'BT' => 'BTN', //Bhutan
'BO' => 'BOL', //Bolivia
'BQ' => 'BES', //Bonaire, Saint Estatius and Saba
'BA' => 'BIH', //Bosnia and Herzegovina
'BW' => 'BWA', //Botswana
'BV' => 'BVT', //Bouvet Islands
'BR' => 'BRA', //Brazil
'IO' => 'IOT', //British Indian Ocean Territory
'BN' => 'BRN', //Brunei
'BG' => 'BGR', //Bulgaria
'BF' => 'BFA', //Burkina Faso
'BI' => 'BDI', //Burundi
'KH' => 'KHM', //Cambodia
'CM' => 'CMR', //Cameroon
'CA' => 'CAN', //Canada
'CV' => 'CPV', //Cape Verde
'KY' => 'CYM', //Cayman Islands
'CF' => 'CAF', //Central African Republic
'TD' => 'TCD', //Chad
'CL' => 'CHL', //Chile
'CN' => 'CHN', //China
'CX' => 'CXR', //Christmas Island
'CC' => 'CCK', //Cocos (Keeling) Islands
'CO' => 'COL', //Colombia
'KM' => 'COM', //Comoros
'CG' => 'COG', //Congo
'CD' => 'COD', //Congo, Democratic Republic of the
'CK' => 'COK', //Cook Islands
'CR' => 'CRI', //Costa Rica
'CI' => 'CIV', //Côte d\'Ivoire
'HR' => 'HRV', //Croatia
'CU' => 'CUB', //Cuba
'CW' => 'CUW', //Curaçao
'CY' => 'CYP', //Cyprus
'CZ' => 'CZE', //Czech Republic
'DK' => 'DNK', //Denmark
'DJ' => 'DJI', //Djibouti
'DM' => 'DMA', //Dominica
'DO' => 'DOM', //Dominican Republic
'EC' => 'ECU', //Ecuador
'EG' => 'EGY', //Egypt
'SV' => 'SLV', //El Salvador
'GQ' => 'GNQ', //Equatorial Guinea
'ER' => 'ERI', //Eritrea
'EE' => 'EST', //Estonia
'ET' => 'ETH', //Ethiopia
'FK' => 'FLK', //Falkland Islands
'FO' => 'FRO', //Faroe Islands
'FJ' => 'FIJ', //Fiji
'FI' => 'FIN', //Finland
'FR' => 'FRA', //France
'GF' => 'GUF', //French Guiana
'PF' => 'PYF', //French Polynesia
'TF' => 'ATF', //French Southern Territories
'GA' => 'GAB', //Gabon
'GM' => 'GMB', //Gambia
'GE' => 'GEO', //Georgia
'DE' => 'DEU', //Germany
'GH' => 'GHA', //Ghana
'GI' => 'GIB', //Gibraltar
'GR' => 'GRC', //Greece
'GL' => 'GRL', //Greenland
'GD' => 'GRD', //Grenada
'GP' => 'GLP', //Guadeloupe
'GU' => 'GUM', //Guam
'GT' => 'GTM', //Guatemala
'GG' => 'GGY', //Guernsey
'GN' => 'GIN', //Guinea
'GW' => 'GNB', //Guinea-Bissau
'GY' => 'GUY', //Guyana
'HT' => 'HTI', //Haiti
'HM' => 'HMD', //Heard Island and McDonald Islands
'VA' => 'VAT', //Holy See (Vatican City State)
'HN' => 'HND', //Honduras
'HK' => 'HKG', //Hong Kong
'HU' => 'HUN', //Hungary
'IS' => 'ISL', //Iceland
'IN' => 'IND', //India
'ID' => 'IDN', //Indonesia
'IR' => 'IRN', //Iran
'IQ' => 'IRQ', //Iraq
'IE' => 'IRL', //Republic of Ireland
'IM' => 'IMN', //Isle of Man
'IL' => 'ISR', //Israel
'IT' => 'ITA', //Italy
'JM' => 'JAM', //Jamaica
'JP' => 'JPN', //Japan
'JE' => 'JEY', //Jersey
'JO' => 'JOR', //Jordan
'KZ' => 'KAZ', //Kazakhstan
'KE' => 'KEN', //Kenya
'KI' => 'KIR', //Kiribati
'KP' => 'PRK', //Korea, Democratic People\'s Republic of
'KR' => 'KOR', //Korea, Republic of (South)
'KW' => 'KWT', //Kuwait
'KG' => 'KGZ', //Kyrgyzstan
'LA' => 'LAO', //Laos
'LV' => 'LVA', //Latvia
'LB' => 'LBN', //Lebanon
'LS' => 'LSO', //Lesotho
'LR' => 'LBR', //Liberia
'LY' => 'LBY', //Libya
'LI' => 'LIE', //Liechtenstein
'LT' => 'LTU', //Lithuania
'LU' => 'LUX', //Luxembourg
'MO' => 'MAC', //Macao S.A.R., China
'MK' => 'MKD', //Macedonia
'MG' => 'MDG', //Madagascar
'MW' => 'MWI', //Malawi
'MY' => 'MYS', //Malaysia
'MV' => 'MDV', //Maldives
'ML' => 'MLI', //Mali
'MT' => 'MLT', //Malta
'MH' => 'MHL', //Marshall Islands
'MQ' => 'MTQ', //Martinique
'MR' => 'MRT', //Mauritania
'MU' => 'MUS', //Mauritius
'YT' => 'MYT', //Mayotte
'MX' => 'MEX', //Mexico
'FM' => 'FSM', //Micronesia
'MD' => 'MDA', //Moldova
'MC' => 'MCO', //Monaco
'MN' => 'MNG', //Mongolia
'ME' => 'MNE', //Montenegro
'MS' => 'MSR', //Montserrat
'MA' => 'MAR', //Morocco
'MZ' => 'MOZ', //Mozambique
'MM' => 'MMR', //Myanmar
'NA' => 'NAM', //Namibia
'NR' => 'NRU', //Nauru
'NP' => 'NPL', //Nepal
'NL' => 'NLD', //Netherlands
'AN' => 'ANT', //Netherlands Antilles
'NC' => 'NCL', //New Caledonia
'NZ' => 'NZL', //New Zealand
'NI' => 'NIC', //Nicaragua
'NE' => 'NER', //Niger
'NG' => 'NGA', //Nigeria
'NU' => 'NIU', //Niue
'NF' => 'NFK', //Norfolk Island
'MP' => 'MNP', //Northern Mariana Islands
'NO' => 'NOR', //Norway
'OM' => 'OMN', //Oman
'PK' => 'PAK', //Pakistan
'PW' => 'PLW', //Palau
'PS' => 'PSE', //Palestinian Territory
'PA' => 'PAN', //Panama
'PG' => 'PNG', //Papua New Guinea
'PY' => 'PRY', //Paraguay
'PE' => 'PER', //Peru
'PH' => 'PHL', //Philippines
'PN' => 'PCN', //Pitcairn
'PL' => 'POL', //Poland
'PT' => 'PRT', //Portugal
'PR' => 'PRI', //Puerto Rico
'QA' => 'QAT', //Qatar
'RE' => 'REU', //Reunion
'RO' => 'ROU', //Romania
'RU' => 'RUS', //Russia
'RW' => 'RWA', //Rwanda
'BL' => 'BLM', //Saint Barth&eacute;lemy
'SH' => 'SHN', //Saint Helena
'KN' => 'KNA', //Saint Kitts and Nevis
'LC' => 'LCA', //Saint Lucia
'MF' => 'MAF', //Saint Martin (French part)
'SX' => 'SXM', //Sint Maarten / Saint Matin (Dutch part)
'PM' => 'SPM', //Saint Pierre and Miquelon
'VC' => 'VCT', //Saint Vincent and the Grenadines
'WS' => 'WSM', //Samoa
'SM' => 'SMR', //San Marino
'ST' => 'STP', //S&atilde;o Tom&eacute; and Pr&iacute;ncipe
'SA' => 'SAU', //Saudi Arabia
'SN' => 'SEN', //Senegal
'RS' => 'SRB', //Serbia
'SC' => 'SYC', //Seychelles
'SL' => 'SLE', //Sierra Leone
'SG' => 'SGP', //Singapore
'SK' => 'SVK', //Slovakia
'SI' => 'SVN', //Slovenia
'SB' => 'SLB', //Solomon Islands
'SO' => 'SOM', //Somalia
'ZA' => 'ZAF', //South Africa
'GS' => 'SGS', //South Georgia/Sandwich Islands
'SS' => 'SSD', //South Sudan
'ES' => 'ESP', //Spain
'LK' => 'LKA', //Sri Lanka
'SD' => 'SDN', //Sudan
'SR' => 'SUR', //Suriname
'SJ' => 'SJM', //Svalbard and Jan Mayen
'SZ' => 'SWZ', //Swaziland
'SE' => 'SWE', //Sweden
'CH' => 'CHE', //Switzerland
'SY' => 'SYR', //Syria
'TW' => 'TWN', //Taiwan
'TJ' => 'TJK', //Tajikistan
'TZ' => 'TZA', //Tanzania
'TH' => 'THA', //Thailand
'TL' => 'TLS', //Timor-Leste
'TG' => 'TGO', //Togo
'TK' => 'TKL', //Tokelau
'TO' => 'TON', //Tonga
'TT' => 'TTO', //Trinidad and Tobago
'TN' => 'TUN', //Tunisia
'TR' => 'TUR', //Turkey
'TM' => 'TKM', //Turkmenistan
'TC' => 'TCA', //Turks and Caicos Islands
'TV' => 'TUV', //Tuvalu
'UG' => 'UGA', //Uganda
'UA' => 'UKR', //Ukraine
'AE' => 'ARE', //United Arab Emirates
'GB' => 'GBR', //United Kingdom
'US' => 'USA', //United States
'UM' => 'UMI', //United States Minor Outlying Islands
'UY' => 'URY', //Uruguay
'UZ' => 'UZB', //Uzbekistan
'VU' => 'VUT', //Vanuatu
'VE' => 'VEN', //Venezuela
'VN' => 'VNM', //Vietnam
'VG' => 'VGB', //Virgin Islands, British
'VI' => 'VIR', //Virgin Island, U.S.
'WF' => 'WLF', //Wallis and Futuna
'EH' => 'ESH', //Western Sahara
'YE' => 'YEM', //Yemen
'ZM' => 'ZMB', //Zambia
'ZW' => 'ZWE', //Zimbabwe
);
$iso_code = isset( $countries[$country] ) ? $countries[$country] : $country;
return $iso_code;
}
@CunningHat
Copy link

You are a god amongst us. You saved me so much tedious work on this. Thankyou so so much!

@wadetaylornz
Copy link

Hi Kathy,
I've been looking for something like this for a while, but this doesn't seem to automatically change the country code. I'm trying to export orders from woocommerce, but even when this is in the functions, any new orders still come through as a 2 letter code. Should I be calling something specific rather than the normal country code?
Thanks,
Wade.

@chesster23
Copy link

Necesito lo mismo, convertir estos campos para exportar los pedidos de Woocoomerce, pero solamente con añadir este código en snippets no funciona.

I need the same, convert these fields to export Woocoomerce orders, but just adding this code in snippets doesn't work.

@helgatheviking
Copy link
Author

helgatheviking commented Oct 19, 2021

@chesster23 At this point I no longer really recall how I was using this. It is a standalone function for converting a two-letter country code to a 3-letter code but by itself it doesn't do anything. I think too much of Woo is reliant on the 2-letter codes to permanently change all the country codes to 3 letters, so you'd need to write something very specific for your export needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment