Magento 2 – How to Get Country Name from Country Code

country-regionsmagento2model

i want to get country name from country code, i got the country code from the data order like this :

$data = $order->getShippingAddress()->getData();
$countryCode = $data['country_id'];
echo $countryCode;

it will print 'US' or any other country code, is there a way to get the country name from this country code?

Best Answer

Create Block file,

   public function __construct(
            \Magento\Directory\Model\CountryFactory $countryFactory
        ) {
            $this->_countryFactory = $countryFactory;
        }

    public function getCountryname($countryCode){    
        $country = $this->_countryFactory->create()->loadByCode($countryCode);
        return $country->getName();
    }

Call from phtml file,

echo $block->getCountryname($countryCode);