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:
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(); }
That’s it.
Do share the solution with Magento developers out there via social media.
Thank you.