Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento : Get country code by country name

Tags:

php

magento

I have tried to find country code by country name. So, for example, I have "Netherlands", I need to get "NL"

I know there is method to find name form code:

$country_name = Mage::app()->getLocale()->getCountryTranslation($country_code) 

But I need vice versa.
So is there any methods in Magento to resolve it?

like image 531
sergio Avatar asked Dec 11 '22 08:12

sergio


1 Answers

From the other question, this can only be done by looping through the country collection

$countryId = '';
$countryCollection = Mage::getModel('directory/country')->getCollection();
foreach ($countryCollection as $country) {
    if ($countryName == $country->getName()) {
        $countryId = $country->getCountryId();
        break;
    }
}
echo $countryId;

Because of how the data is stored in the XML there is no way to filter or load by name.

like image 152
Steve Robbins Avatar answered Jan 08 '23 19:01

Steve Robbins