Magento 2 - Get country name from country code
-
18-02-2021 - |
Question
I want to get country name from country code, in Magento 1.9 I use the code like this to get country name:
$countryCollection = Mage::getModel('directory/country_api')->items();
foreach($countryCollection as $key=>$val) {
$countryCodes[$val['country_id']] = $val['name'];
}
How I can made this code to work for Magento 2?
I get the values from the multiselect attribute with the name country_iso_list, the values of this attribute are AD, AE, AF, AG ...ZW, so they are countries code. I just want to replace the country code with the country name in the frontend. So all I just need is to replace the this part of code:
echo $countryCodes[$val];
in this code
$data = explode(',',$block->getProduct()->getData('hed_country_iso_list'));
foreach($data as $value):
$attr = $block->getProduct()->getResource()->getAttribute('country_iso_list');
if ($attr->usesSource()):
$countries = $attr->getSource()->getOptionText($value);
$ccode = explode(', ', $countries);
endif;
endforeach;
if (!empty($countries)):
echo "<p>"./* @escapeNotVerified */ __('This product can be sent to the following countries:')."</p>";
$k=0;
echo "<p>";
foreach($ccode as $key=>$val) {
echo $countryCodes[$val];
echo (++$k < sizeof($ccode)) ? ', ' : '';
}
echo "</p>";
endif;
Thank you
Solution
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$country = $objectManager->create('\Magento\Directory\Model\Country')->load('AE')->getName();
It is an example replace your country code with 'AE'
or To get country collection according to store, you need to add following code in your file:
<?php
namespace Vendor\YourModule\Block;
class country extends \Magento\Framework\View\Element\Template {
protected $_countryCollectionFactory;
public function __construct(
\Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory,
\Magento\Framework\View\Element\Template\Context $context,
array $data = []
) {
$this->Customer = $customer;
parent::__construct($context, $data);
$this->_countryCollectionFactory = $countryCollectionFactory;
}
public function getCountryCollection()
{
$collection = $this->_countryCollectionFactory->create()->loadByStore();
return $collection;
}
/**
* Retrieve list of top destinations countries
*
* @return array
*/
protected function getTopDestinations()
{
$destinations = (string)$this->_scopeConfig->getValue(
'general/country/destinations',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
return !empty($destinations) ? explode(',', $destinations) : [];
}
/**
* Retrieve list of countries in array option
*
* @return array
*/
public function getCountries()
{
return $options = $this->getCountryCollection()
->setForegroundCountries($this->getTopDestinations())
->toOptionArray();
}
}
Another simple way of doing it
Add code in your controller :
protected $_country;
public function __construct(
\Magento\Directory\Model\Country $country
) {
$this->_country = $country;
}
public function getCountryname() {
$countryCollection = $this->_country->getCollection();
foreach ($countryCollection as $country) {
echo "Country name:".$country->getName();
echo "Country code:".$country->getCountryId();
}
}
I hope this will help
OTHER TIPS
Try below code.
public function __construct(
\Magento\Directory\Model\CountryFactory $countryFactory
)
{
$this->countryFactory = $countryFactory;
}
public function getCountryName($code)
{
$_country = $this->countryFactory->create()->loadByCode($code);
if($_country)
{
return $_country->getName();
}
return false;
}