skip to Main Content

I hope you are doing well.

I want to get all the countries in the below format.

$countries = [
  "AU" => "Australia",
  "IN" => "India",
  "US" => "United States",
  "UK" => "United Kingdom",
  .......
]

I tried but no use, i am always getting CountryId as 2 digit names like

$countries = [
  0 => "AU",
  1 => "IN",
  2 => "US",
  3 => "UK",
  .......
]

I have used the following code

MagentoDirectoryModelResourceModelCountryCollection::loadByStore()

How can i achieve this in Magento 2 ?

Thanks in advance.

2

Answers


  1. I think you need to loop through the country collection and create a new array with the country code as key and the country name as value. Could you try something like this?

    public function __construct(
        MagentoBackendBlockTemplateContext $context,
        MagentoDirectoryModelResourceModelCountryCollectionFactory $countryCollectionFactory,
        MagentoDirectoryModelCountryFactory $countryFactory,
        array $data = []
    ) {
        $this->countryCollectionFactory = $countryCollectionFactory;
        $this->countryFactory = $countryFactory;
        parent::__construct($context, $data);
    }
    
    
    public function getCountryName($countryCode){
        $country = $this->countryFactory->create()->loadByCode($countryCode);
        return $country->getName();
    }
    
    public function getCountryCollection()
    {
        $collection = $this->countryCollectionFactory->create()->loadByStore();
        return $collection;
    }
    
    public function getCountries()
    {
        $countryCollection = $this->getCountryCollection();
    
        $countries = [];
        foreach ($countryCollection->getData() as $country) {
            $countries[$country['country_id']] = $this->getCountryName($country['country_id']);
        }
    
        return $countries;
    }
    

    Then in your template you can get the countries with $block->getCountries().

    Result:

    array(249) { ["AD"]=> string(7) "Andorra" ["AE"]=> string(28) "Verenigde Arabische Emiraten" ["AF"]=> string(11) "Afghanistan" ["AG"]=> string(18) "Antigua en Barbuda" ["AI"]=> string(8) "Anguilla" ["AL"]=> string(8) "Albanië" ["AM"]=> string(8) "Armenië" ["AN"]=> NULL ["AO"]=> string(6) "Angola" ["AQ"]=> string(10) "Antarctica" ["AR"]=> string(11) "Argentinië" ["AS"]=> string(16) "Amerikaans-Samoa" ["AT"]=> string(10) "Oostenrijk" ["AU"]=> string(10) "Australië" ["AW"]=> string(5) "Aruba" ["AX"]=> string(6) "Åland" ...
    
    Login or Signup to reply.
  2. Use my simple code: (can ez injection class)

        public function getCountries()
        {
            /** @var MagentoDirectoryModelResourceModelCountryCollectionFactory $countryCollectionFactory */
            $countryCollectionFactory = $this->_objectManager->get(MagentoDirectoryModelResourceModelCountryCollectionFactory::class);
    
            /** @var MagentoDirectoryModelResourceModelCountryCollection $countryCollection */
            $countryCollection = $countryCollectionFactory->create();
    
            $countryCollection = $countryCollection->toOptionArray();
    
            return array_column($countryCollection, 'label', 'value');
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search