6

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()
            ->loadByCode($code,$country_id)->getFirstItem();
...
}
//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

11

This has to work

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

Try below code:

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

Output:

Array
(
    [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.

3
  • 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
0
<?php
    namespace MODULENAMESPACE\MODULENAME\Helper;

    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) {
                    $this->_logger->critical($e->getMessage());
                    return false;
                }
            }
    }
?>

* Get code *

call helper
<?php
    $helper = $this->dataHelper; //construct it before call
    $helper->getRegionDataByCode('TX')->getRegionId();
    //[region_id],[country_id],[code],[default_name]
?>

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.