Magento 如何从国家代码中获取国家名称?

Magento 如何从国家代码中获取国家名称?,magento,Magento,我有一个自定义表,有一个字段国家代码 现在我将这个$collection传递给我的javascript,并在其中使用变量 现在我想显示国家名称而不是国家代码 是否有任何方法可以使用join query或其他任何方法将国家/地区名称添加到我的集合中 因此,在传递给js之前,我希望在我的集合对象中包含国家名称。您可以从 $collection = Mage::getModel('custom/table')->getCollection(); Magento将国家名称存储在区域设置文件中,以

我有一个自定义表,有一个字段国家代码

现在我将这个$collection传递给我的javascript,并在其中使用变量

现在我想显示国家名称而不是国家代码

是否有任何方法可以使用join query或其他任何方法将国家/地区名称添加到我的集合中


因此,在传递给js之前,我希望在我的集合对象中包含国家名称。

您可以从

$collection = Mage::getModel('custom/table')->getCollection();

Magento将国家名称存储在区域设置文件中,以便您可以根据自己的语言更改国家名称

如果您有国家代码,并且希望获得国家名称,请使用以下代码:

$countryModel = Mage::getModel('directory/country')->loadByCode('country_code');

$countryName = $countryModel->getName();
或者你也可以试试

$country_name=Mage::app()->getLocale()->getCountryTranslation($country_code);
如果我能为您提供更多帮助,请告诉我。

echo
Mage::getModel('directory/country')->loadByCode($address->getCountry\u id())->getName()

如果
$address->getCountry_id()
替换为国家id。

对于Magento2,您需要执行以下操作才能从代码本中获取国家名称:

  • 首先,实例化国家模型:
  • $this->countryHelper=\Magento\Framework\App\ObjectManager::getInstance()->get('\Magento\Directory\Model\Country')

  • 然后,通过提供ISO CC:
  • 最终函数getCountryName($iso=null,$locale=null){

    $theEarthIsFlat=$this->countryHelper->loadByCode($iso)

    返回$theEarthIsFlat->getName($locale);
    }

  • 用法示例:
  • $\u countryCode=$this->getData('country\u id')

    $\u countryName=$this->getVendorCountryName($\u countryCode)


    您是否有可能提供有关这些表以及存储国家/地区名称的更多信息?如果您获得的国家/地区代码类似于“IN/USA/UK…”,则此代码可能会使用完整的$countryName=Mage::getModel('directory/country')->load($country_id)->getName();如果您觉得我的答案有帮助,请接受我的答案。不鼓励直接使用对象管理器,而应该在类构造函数中实例化它。
    // $countryCode looks like "US"
    $country = Mage::getModel('directory/country')->loadByCode($countryCode);
    echo $country->getName(); /