How To Get Region ID By Region & Country Code In Magento 2
Magento 2 store owners implement various tactics based on different conditions to maximize profits.
For example, a store owner can offer discounts on shipping to certain regions.
However, to implement this condition, the developer needs to get region ID by region & country code in Magento 2. The below code shows how to do so.
Another scenario where this solution can be useful is when the customer address is to be collected. Also get region code by region ID in Magento 2 which is used to retrieve the specific region code associated with a given region ID.
Method To Get Region ID By Region & Country Code In Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
use Magento\Directory\Model\RegionFactory; protected $regionFactory; public function __construct(RegionFactory $regionFactory) { $this->regionFactory = $regionFactory; } public function getRegionId($stateCode, $countryId) { return $this->regionFactory->loadByCode($stateCode, $countryId)->getRegionId(); } |
Any doubts in the above code? Please feel free to mention them in the Comments section below. I’d try to help you out asap.
Do share the solution with Magento developers out there via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
Magento Announces Maintenance Scheduled For March 06 – 08 [2020]
Meetanshi Magento Extensions Launches and Updates January [2020]
Next