Magento - Get Country Id from Name - is there a easier/quicker way?

后端 未结 2 326
慢半拍i
慢半拍i 2021-01-13 04:34

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 fr         


        
相关标签:
2条回答
  • 2021-01-13 05:09

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

    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.

    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.

    0 讨论(0)
  • 2021-01-13 05:09

    There is no other way, because of translations. This is function for getting country name, you can't reverse it

    public function getName()
    {
        if(!$this->getData('name')) {
            $this->setData(
                'name',
                Mage::app()->getLocale()->getCountryTranslation($this->getId())
            );
        }
        return $this->getData('name');
    }
    
    0 讨论(0)
提交回复
热议问题