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

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

45 mins ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

3 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

5 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

5 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

6 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

7 days ago