How To

How to Get Region Code by Region ID in Magento 2

Hello Magento Friends,

How are you all? Today I am here to demonstrate How to Get Region Code by Region ID in Magento 2. By chance, you missed out on our previous blog, How to Create QR Code of invoice ID and add it to Magento 2 invoice PDF.

Introduction:

In today’s world, it is possible to deliver products to any corner. Magento provides the functionality to run a multinational functioning store. At the time of building delivery and payment related extensions or customization, there is a need for region details in Magento 2. You can get Region Code by Region ID in Magento 2.

Here we are getting region code with the help of the order Increment Id method.

Steps to Get Region Code by Region ID in Magento 2:

Step 1: Go to the below path

app\code\Vendor\Extension\Helper\Data.php

And add the code as shown below.

<?php
namespace Vendor\Extension\Helper;

use Magento\Directory\Model\RegionFactory;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Sales\Model\OrderFactory;

class Data extends AbstractHelper
{
    protected $orderFactory;
    protected $regionFactory;

    public function __construct(
        Context $context,
        OrderFactory $orderFactory,
        RegionFactory $regionFactory
    )
    {
        $this->orderFactory = $orderFactory;
        $this->regionFactory = $regionFactory;
        parent::__construct($context);
    }

    public function getOrderRegionData()
    {
       // load order using order increment id
        $orderIncrementId = 000000010;
        $order = $this->orderFactory->create()->loadByIncrementId($orderIncrementId);

        $billingAddress = $order->getBillingAddress()->getData();
        $region = $this->regionFactory->create()->load($billingAddress['region_id']);

        // to get all region data
        $this->_logger->info(print_r($region->getData(), true));

        return $region->getCode();
        
    }
}

The result of this we will get in an array format as shown below:

Array (
[region_id] => 12
[country_id] => US
[code] => CA
[default_name] => California
[name] => California
)

This way you will get the state code, state name, and use it wherever required.

Conclusion:

Therefore, this way you can Get Region Code by Region ID in Magento 2. If you have any trouble with the execution of the above steps, mention it in the comment part. Make sure to share the article amongst your Magento friends and keep in touch with us.

Happy Coding

Click to rate this post!
[Total: 4 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

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

3 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

5 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

7 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago