How To Get Region Code By Region Name In Magento 2

How To Get Region Code By Region Name In Magento 2

The post gives the programmatic method to get region code by region name in Magento 2.

One can use the below code when you want to impose conditions based on region. For example, restrict a user to place an order for delivery in specific regions. So you pass the region code of the region name entered by the frontend user.

In Magento 2, region codes are the internal primary keys in the database that identifies the region. For example, AL is the region code for Alabama.

Method to Get Region Code by Region Name in Magento 2:

Create RegionCode.php Class in app/code/Vendor/Extension/Class

Now you can access getRegionCode() function as per your requirement by extending GetRegionCode class

That’s it.

Any doubts on how to get region code from region name for Magento 2 store? Please feel free to mention them in the Comments section below. I’d be there to solve them asap!

I’d be grateful if my readers share the solution with fellow Magento developers via social media.

Thank you.

(based on 12 Reviews)

Sanjay is a co-founder at Meetanshi. He is a Certified Magento Developer who loves creating Magento E-commerce solutions. Owing to his contributions in Magento Forums and posting solutions, he is among the top 50 contributors of the Magento community in 2019. When he is not engrossed with anything related to Magento, he loves to play cricket.

Leave a Reply

(based on 5 Reviews)