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.
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.
// 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!!
Write an article about ecommerce that help people to grow their ecommerce business. You’ll find best ecommerce guide, news, tips & more!



Leave a Reply