Magento: enter country code by country name

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

I know there is a way to find the name form code:

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

But I need the other way around.
So, are there any methods in Magento to solve it?

+4
source share
5 answers

From another question , this can only be done by cycling through the village collection.

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

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

+5
source

It works for me $list = Mage::app()->getLocale()->getCountryTranslationList(); $list=array_flip($list); echo $list['United States'];

+5
source

. , , XML, XML .

$list = Mage::app()->getLocale()->getCountryTranslationList();
foreach ($list as $id => $name) {
    if ($name == $countryName) {
        return $id;
    }
}

, . :

$oldCode = Mage::app()->getLocale()->getLocaleCode();
Mage::app()->getLocale()->setLocaleCode('en_US');
...
...
...
Mage::app()->getLocale()->setLocaleCode($oldCode);
+2

        <select title="Country" class="validate-select" id="billing:country_id"                      name="billing[country_id]">
             <option value=""> </option>
             <?php
                 $collection = Mage::getModel('directory/country')->getCollection();    
                 foreach ($collection as $country) 
                 {
                    ?>
                     <option value="<?php echo $country->getId(); ?>"><?php echo $country->getName(); ?></option>
                    <?php 
                }
            ?>
       </select>
0

Magento 2 , Zend Framework, :

$countryId = array_search($countryName, \Zend_Locale::getTranslationList('territory'));

, .

0

Source: https://habr.com/ru/post/1524304/


All Articles