How to Get All Regions/States of Specific Country by Country Code in Magento 2
Steps to Get All Regions/States of Specific Country by Country Code in Magento 2:
Step 1: We are getting data in the root script. So go to the below path
magento_root_directory\pub\getRegions.php
And add the code as follows
<?php
use \Magento\Framework\AppInterface;
try
{
require __DIR__ . '/../app/bootstrap.php';
}
catch (\Exception $e)
{
echo 'Autoload error: ' . $e->getMessage();
exit(1);
}
try
{
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');
$countryModel = $objectManager->get('Magento\Directory\Model\Country');
$countryCode = 'IN';
$regionData = $countryModel->loadByCode($countryCode)->getRegions()->loadData()->toArray();
echo "<pre>";
print_r($regionData);
}
catch(\Exception $e)
{
print_r($e->getMessage());
}
Comments
Post a Comment