{"id":1505,"date":"2021-01-02T09:37:47","date_gmt":"2021-01-02T09:37:47","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/get-region-code-by-region-id-magento-2\/"},"modified":"2025-06-02T14:23:15","modified_gmt":"2025-06-02T08:53:15","slug":"get-region-code-by-region-id-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-region-code-by-region-id-magento-2\/","title":{"rendered":"How to Get Region Code by Region ID in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In the world of drone delivery, there is no boundary, and businesses can deliver products and services to the customers around the corner. With Magento, one can run an internationally operated online store.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In most instances, while developing extensions or customization based on delivery and payment gateway, you need to avail the region data. Earlier, I provided the solution to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-get-region-id-by-region-country-code\/\">get region ID by region &amp; country code in Magento 2<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Generally, it is a small matter of task, if the requirement is related to the region name. However, it\u2019s a little bit tricky when the need arrives to\u00a0get region code by region ID in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ever faced an issue for\u00a0getting a region code by region ID\u00a0? The below code shows the steps to get region code by region ID in Magento 2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Get Region Code by Region ID in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1.Create&nbsp;<strong>Data.php<\/strong>&nbsp;in&nbsp;<strong><strong>Vendor\\Extension\\Helper<\/strong>&nbsp;<\/strong>and use the following solution.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nuse Magento\\Directory\\Model\\RegionFactory;\nuse Magento\\Framework\\App\\Helper\\AbstractHelper;\nuse Magento\\Framework\\App\\Helper\\Context;\nuse Magento\\Sales\\Model\\OrderFactory;\n\nclass Data extends AbstractHelper\n{\n    protected $orderFactory;\n    protected $regionFactory;\n\n    public function __construct(\n        Context $context,\n        OrderFactory $orderFactory,\n        RegionFactory $regionFactory\n    )\n    {\n        $this->orderFactory = $orderFactory;\n        $this->regionFactory = $regionFactory;\n        parent::__construct($context);\n    }\n\n    public function getOrderRegionData()\n    {\n        $orderId = 3;\n        \/\/ load order using order id\n        $order = $this->orderFactory->create()->load($orderId);\n        \/\/ OR load order using order increment id\n        $orderIncrementId = 00022;\n        $order = $this->orderFactory->create()->loadByIncrementId($orderIncrementId);\n\n        $billingAddress = $order->getBillingAddress()->getData();\n        $region = $this->regionFactory->create()->load($billingAddress['region_id']);\n\n        \/\/ to get\/print all region data\n        $this->_logger->info(print_r($region->getData(), true));\n\n        return $region->getCode();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this code, two methods are mentioned to load order. Just use with which you\u2019re comfortable!<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Using order ID<\/li>\n\n\n\n<li>Using order increment ID<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Here the returned region code is&nbsp;<strong>\u2018<\/strong><strong>AN\u2019<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>$this-&gt;_logger-&gt;log(print_r($region-&gt;getData(), true))&nbsp;<\/strong>prints array log as mentioned below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Array (\n[region_id] => 533\n[country_id] => IN\n[code] => AN\n[default_name] => Andaman and Nicobar Islands\n[name] => Andaman and Nicobar Islands\n)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Just use this returned array in the code where you required to get state code, state name, and other requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Quite catchy but solved!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Don\u2019t forget to share the solution with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the world of drone delivery, there is no boundary, and businesses can deliver products and services to the customers around the corner. With Magento,&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1505","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1505","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1505"}],"version-history":[{"count":5,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1505\/revisions"}],"predecessor-version":[{"id":16274,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1505\/revisions\/16274"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}