Get country code from country name in Magento

Tech Bandhu

Its very simple to get the country name by given country code with following code,

$countryName = Mage::getModel("directory/country")->load("in")->getName();

But in one of the application I come across the situation where I need get country code from country name. I have used bellow logic to fulfill my need,

$countryList = Mage::getResourceModel('directory/country_collection')
$needle = "India";
foreach ($countryList as $key => $val) 
   if (strtolower($val['label']) === strtolower($needle)) {
       echo $val['value'];

You will get the array of all country list with in $countryList variable. From that array I am searching for my required country code.

I am sure there must be some alternate way to get the same. If you know some alternate way, which is more convenient you are most welcome to post your ideas or suggestions.

View original post


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s