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
有帮助吗?

解决方案

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);

其他提示

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);
许可以下: CC-BY-SA归因
scroll top