简体   繁体   English

Magento:按国家/地区名称获取国家/地区代码

[英]Magento : Get country code by country name

I have tried to find country code by country name. 我试图按国家/地区名称查找国家/地区代码。 So, for example, I have "Netherlands", I need to get "NL" 所以,例如,我有“荷兰”,我需要得到“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? 那么Magento有什么方法可以解决它吗?

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. 由于数据如何存储在XML中,因此无法按名称进行过滤或加载。

This works for me $list = Mage::app()->getLocale()->getCountryTranslationList(); $list=array_flip($list); echo $list['United States']; 这适用于我$list = Mage::app()->getLocale()->getCountryTranslationList(); $list=array_flip($list); echo $list['United States']; $list = Mage::app()->getLocale()->getCountryTranslationList(); $list=array_flip($list); echo $list['United States'];

There is a way to get Country ID without loading countries collection. 有一种方法可以获取国家ID而无需加载国家/地区集合。 As all country IDs mapped with names are stored in XML you can access that XML by locale object. 由于所有使用名称映射的国家/地区ID都存储在XML中,因此您可以通过区域设置对象访问该XML。

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

Please note that country name should be translated to the current locale language. 请注意,国家/地区名称应翻译为当前的区域设置语言。 Otherwise change locale to necessary one: 否则将语言环境更改为必要的:

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

in Magento 2 there is a lack of simple processes so i suggest you to use the zend framework like : 在Magento 2中缺少简单的进程,所以我建议你使用zend框架,如:

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

Don't forget to make sure your country name is capitalized. 不要忘记确保您的国家/地区名称大写。

the country collection 国家集合

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM