How To

How to Get All Regions/States of Specific Country by Country Code in Magento 2?

Hello Magento Friends,

This Magento tutorial will help you Get All Regions/States of Specific Country by Country Code in Magento 2.

In your Magento 2 store, many regions and states are added to a specific country. You can easily add a new Region to a specific Country when required. When you go on adding new regions, you do not remember which region a particular country holds. For that, you need to have a list of all regions/states assigned to a specific country in Magento 2.

You can use the below method to get the array of all the regions/states of a specific country 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());
}

Conclusion:

This way, you can fetch the list of all regions/states of a specific country by country code in Magento 2. You can also integrate Magento 2 City and Region Manager Extension to manage states, regions, and zip codes easily.

If you face difficulty getting all regions/states of a specific country by country code in Magento 2. Share the tutorial with your friends and stay updated with us so that you do not miss out on any important solution.

Happy Coding!

Click to rate this post!
[Total: 1 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

1 day ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

1 day ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago