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.
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.
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.
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
Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…
In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…
In the world of business, understanding financial metrics is crucial for making informed decisions and…
Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…
In this era, businesses are always on the lookout for ways to engage with their…
Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…