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.
Contents
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()); }
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!
In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…
In the world of business, understanding financial metrics is crucial for making informed decisions and…
Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…
In this era, businesses are always on the lookout for ways to engage with their…
Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…
E-commerce has transformed the way consumers shop for products and services and interact with businesses.…