How to get region code by region id in Magento 2?

How to get region code by region id in Magento 2 – Most of the time in Magento based development. We need to get the region / State details to show in front end or emails or admin grids. Get the region details by id is very common searching terms on the internet for Magento developers.

To get these details you can simply use RegionFactory wherever in your controller, helper classes. You can use wherever according to your need arise. Let’s see how we can get the use of this below.

Above are the must and basic structure of the class. The method getRegionData will get the $regionId as the parameter and return the region data as an array. So our return array is looks like below.

With this returned array, you can get state code, state name, and use according to your requirement. This was tested in the Magento 2.3.3 version.

In this article, we show how to get the get region code by region id with the piece of the example code. You may interest to read my previous article on update the shipping address fields in the checkout using LayoutProcessor Magento 2. And also Magento 2 create order attribute.

Kindly share your thoughts to improve my writing. And please share this post with your professional networks.

1 thought on “How to get region code by region id in Magento 2?”

Leave a Reply

Your email address will not be published. Required fields are marked *