In this post I am going to explain how to get all regions of country by country code Magento 2. Add below code snippet in Block class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | protected $_country; public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Directory\Model\Country $country, array $data = [] ) { $this->_country = $country; parent::__construct($context, $data); } // Get All Regions of Country public function getAllRegionsOfCountry($countryCode) { $regionCollection = $this->_country->loadByCode($countryCode)->getRegions(); $regions = $regionCollection->loadData()->toOptionArray(false); return $regions; } |
Add below code snippet in template file.
1 2 3 4 5 6 7 8 9 | // Country Code $countryCode = 'in'; // Get All Regions of Country $regions = $block->getAllRegionsOfCountry($countryCode); echo "<pre>"; print_r($regions); echo "</pre>"; |
Thats it. Enjoy Magento 2!! Team HiddenTechiesWrite an article about ecommerce that help people to grow their ecommerce business….
Recent Comments