Magento - 从名称中获取国家/地区 ID - 有更简单/更快的方法吗? [英] Magento - Get Country Id from Name - is there a easier/quicker way?

查看:23
本文介绍了Magento - 从名称中获取国家/地区 ID - 有更简单/更快的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为给定的国家/地区名称加载 2 个字母的 ISO2 国家/地区 ID.

I need to load the 2 letter ISO2 country ID for a given country name.

现在,我使用以下方法来做到这一点:

Right now, I use the following method to do this:

// Method to Get countryId from CountryName
function getCountryId($countryName) {
    $countryId = '';
    $countryCollection = Mage::getModel('directory/country')->getCollection();
    foreach ($countryCollection as $country) {
        if ($countryName == $country->getName()) {
            $countryId = $country->getCountryId();
            break;
        }
    }
    $countryCollection = null;
    return $countryId;
}

用法:

var_dump(getCountryId('Germany'));

输出:

string(2) "DE"

是否有更简单/更快的方法来执行此操作,而不是每次加载国家/地区集合并对其进行迭代?

Is there a easier/quicker way to do this instead of loading the country collection and iterating through it every time?

推荐答案

令人惊讶的是,循环是实现此目的的唯一方法.

Surprisingly, looping is the only way you'll achieve this.

国家/地区名称存储在 lib/Zend/Locale/Data/ 中的 XML 文件中,它们按区域设置(en、es、fr)和国家/地区代码组织,而不是国家/地区名称.

The country names are stored in XML files in lib/Zend/Locale/Data/ and they're organized by locale (en, es, fr) and then country code, not country name.

由于它不是 SQL 表,因此您不能添加 WHERE 子句(使用 Magento 的 addFieldToFilter()),因此无论如何您都会遍历 XML 节点.

Since it's not a SQL table you can't add a WHERE clause (using Magento's addFieldToFilter()), so you'll be looping through the XML nodes anyway.

这篇关于Magento - 从名称中获取国家/地区 ID - 有更简单/更快的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆