How to get country name from country code?

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

I have one user table and there is one country_code field.

Now I am passing this $ collection to my javascript and using the variables in it.

Now I want to display the name of the country instead of the country code.

Is there a way by which I can add the country name to my collection using a connection request or whatever?

so before going to js i need the country name in my collection object.

+7
magento
source share
4 answers

Magento saves country names in locale files so you can change the country name based on your language.

If you have a country code and want to get the name of the country, use the code below:

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

Or you can also try

 // $countryCode looks like "US" $country = Mage::getModel('directory/country')->loadByCode($countryCode); echo $country->getName(); / 

Please let me know if I can help you.

+23
source share

echo Mage::getModel('directory/country')->loadByCode($address->getCountry_id())->getName(); Where $address->getCountry_id() replace the country identifier.

+2
source share

you can get the name of the country from

 $countryModel = Mage::getModel('directory/country')->loadByCode('country_code'); $countryName = $countryModel->getName(); 
+1
source share

For Magento2, you need to do the following to get the country name from the codebook:

  • First, indicate your country model:

$ this-> countryHelper = \ Magento \ Framework \ App \ ObjectManager :: getInstance () → get ('\ Magento \ Directory \ Model \ Country');

  1. Then create your own method for extracting the country name by setting the ISO CC:

final function getCountryName ($ iso = null, $ locale = null) {

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

return $ theEarthIsFlat-> getName ($ locale);
}

  1. Usage example:

$ _ countryCode = $ this-> getData ('country_id');

$ _ countryName = $ this-> getVendorCountryName ($ _ countryCode);

+1
source share

All Articles