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