Magento 2 Get a Country Numeric Code Using Country Code

Magento 2 use ISO 3166-1 two-letter country code as a value of country dropdown, but sometimes you may need a three-digit numeric code (numeric-3) and this is not provided by Magento. There is no way in the Magento to get country numeric code using country two-letter code.

We were creating an API module for the Magento 2 website, in this module, we must have to pass 3 digit numeric code as country name and Magento gives either 2 or 3 letters country code but not numeric code, so to fulfil this requirement of API module we developed this small module to get three-digit country numeric code using two-letter country code in the Magento 2.

How to get a module of Magento 2 ISO 3166-1 Country code:

You can download or clone Dhairvi Magento 2 ISO 3166-1 Country Code module using our git repo.
https://github.com/vish-vaghela/magento-ISO3166

How to Use:

After installing the Dhairvi Magento 2 ISO 3166-1 module on your Magento 2 website you can inject their class on your file as below.

public function __construct(
    \Dhairvi\ISO3166\Model\Collection $cntCollection
) {
    $this->iso3166Collection = $cntCollection;
}

You can call our class \Dhairvi\ISO3166\Model\Collection to any file of Magento like module file, block, controller, observer, plugins, phtml etc.

Now you can get different values of the country based on different parameters as per your requirements. Following are some examples.

  • How to get country 3 digits numeric code using 2 letter country code in Magento 2.
/**
 * Get country three-digit numeric code by two-letter country code.
 */
public function getCountryNumericCodeByCountryTwoDigitCode($countryCode)
{
    $country3DigitNumericCode = $this->iso3166Collection->getNumericCodeByCountryCode(strtoupper($countryCode));
    return $country3DigitNumericCode;
    
    //If $countryCode = 'US' then  output = 840
}
  • How to get country 3 digits numeric code using 3 letter country code in Magento 2.
/**
 * Get country three-digit numeric by three-letter country code.
 */
public function getCountryNumericCodeByCountryThreeDigitCode($countryCode)
{
    $country3DigitNumericCode = $this->iso3166Collection->getNumericCodeByCountryCode3(strtoupper($countryCode));
    return $country3DigitNumericCode;

    //If $countryCode = 'USA' then  output = 840
}
  • How to get 2 letter country code using country 3 digits numeric code.
/**
 * Get two-letter country code by country three-digit numeric code.
 */
public function getCountryTwoDigitCodeByNumericCode($numericCode)
{
    $country3DigitNumericCode = $this->iso3166Collection->getTwoDigitCountryCodeByNumericCode($numericCode);
    return $country3DigitNumericCode;

    //If $countryCode = '840' then  output = US
}
  • How to get 3 letter country code using country 3 digits numeric code.
/**
 * Get three-letter country code by country three-digit numeric code.
 */
public function getCountryThreeDigitCodeByNumericCode($numericCode)
{
    $country3DigitNumericCode = $this->iso3166Collection->getThreeDigitCountryCodeByNumericCode($numericCode);
    return $country3DigitNumericCode;

    //If $countryCode = '840' then  output = USA
}
  • How to country name by 3 digit numeric code.
 /**
 * Get country Name by country three-digit numeric code.
 */
public function getCountryNameByNumericCode($numericCode)
{
    $country3DigitNumericCode = $this->iso3166Collection->getCountryNamebyNumericCode($numericCode);
    return $country3DigitNumericCode;

    //If $countryCode = '840' then  output = United States Of America
}