I need to get the region_id given the region_code and the country_id, but I cannot manage to achieve it. I've tried using the countryFactory and the regionFactory, but i do not understand what i'm doing wrong.

I have something like this

in the constructor the RegionFactory

public function __construct(
    \Magento\Directory\Model\RegionFactory $regionFactory
) {
    $this->_regionFactory = $regionFactory;

function getRegionIdByCode($code){

 $region = $this->_regionFactory->create()
//where code is correctly calculated based on a csv with all italian regions

Cannot manage to work with the $region object. I need to do something like $region->getRegionId()

Is it possible??

3 Answers 3


This has to work

$region = $this->_regionFactory->create();
$regionId = $region->loadByCode($code, $country_id)->getId();

Try below code:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$region = $objectManager->create('Magento\Directory\Model\Region')
                        ->loadByCode('CA', 'US');


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

Note: Please do not use Object Manager. Instead create function and then call it.

  • i accepted the @Nikolas solution, as it is the one not using object manager, but regionFactory correctly as I needed, even though this is going to work as well and can also be translated with regionFactory like this $region = $this->_regionFactory->create() ->loadByCode($code,$country_id)->getData("region_id"); But a +1 for the suggestion
    – sissy
    Commented Oct 17, 2017 at 7:17
  • How get all region code and names from country id? Commented Sep 1, 2018 at 6:18
  • Any help thanks magento.stackexchange.com/q/325882/57334 @Abhishek Panchal
    – zus
    Commented Nov 6, 2020 at 7:41

    use \Magento\Directory\Model\RegionFactory;
    use \Psr\Log\LoggerInterface;

    class Data extends AbstractHelper {
           protected $_regionFactory;
           protected $_logger;

           public function __construct(
              RegionFactory $regionFactory,
              LoggerInterface $logger
              $this->_regionFactory = $regionFactory;
              $this->_logger = $logger;

             * Gets the region data from the two digit state code
             * @param $state - Two letter state code
             * @return array [region_id],[country_id],[code],[default_name],[name]

            public function getRegionDataByCode($state, $countryId = 'US') {
                try {
                    $region   = $this->_regionFactory->create();
                    $regionData = $region->loadByCode($state, $countryId);
                    return $regionData;
                catch(\Exception $e) {
                    return false;

* Get code *

call helper
    $helper = $this->dataHelper; //construct it before call

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.