4

I want to get country_id from the country name in Magento, Please suggest how can I achieve this. I found these database tables but they are not helpful:

  • directory_country
  • directory_country_region
  • directory_country_region_name

2 Answers 2

2

You can use below code for that

$countryName = 'your country name';
$countryId = '';
$countryCollection = Mage::getModel('directory/country')->getCollection();
foreach ($countryCollection as $country) {
    if ($countryName == $country->getName()) {
        $countryId = $country->getCountryId();
        break;
    }
}
$countryCollection = null;
print_r($countryId);
4
  • 1
    I don't think that loading a full collection is the best idea in this case even if it works.
    – Marius
    Sep 19, 2016 at 12:29
  • I dont know any other way ? if you know please post an answer I also take benifit and learn something from masters like you Sep 19, 2016 at 12:31
  • @MurtuzaZabuawala, It works for me. Thanks.
    – Yogita
    Sep 19, 2016 at 12:32
  • You are always Welcome :) Sep 19, 2016 at 12:32
7

The country names don't come from the database.
They come from the ZF data xmls.
You can see what this returns:

Mage::app()->getLocale()->getCountryTranslationList();  

Not sure about it, but it should return a list like this:

array(
   'AF' => 'Afghanistan', 
   'AG' => 'Antigua and Barbuda',
   ....
   'ZW' => 'Zimbabwe'
); 

If this is true, you can just get it like this:

$countryName = 'your country name here';
$countries = Mage::app()->getLocale()->getCountryTranslationList();;
$countryCode = array_search($countryName, $countries);
2
  • 1
    Great way though. you are real genious Sep 19, 2016 at 12:34
  • @Marius, Thanks. This is also other way to achieve this.
    – Yogita
    Sep 19, 2016 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.