2

I am trying to get state code form Region ID. e.g. If my region id = 12 state code should be CA. This is what I am doing right now.

$regionObject = $objectManager->get('Magento\Directory\Model\RegionFactory');
$regionModel = $regionObject->create();
$region = $regionModel->loadByCode($regionId, $countryCode);
$shipFromState = $region->getName();

I am trying to get shipping origin address. If there is a good way to get the address that will be nice. Currently I am getting shipping information using following code

$scopeConfig = \Magento\Framework\App\ObjectManager::getInstance()
                ->get('\Magento\Framework\App\Config\ScopeConfigInterface');
$regionId = $scopeConfig->getValue('shipping/origin/region_id', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

3 Answers 3

2

Try this one:

    $_regionFactory- = $objectManager->get('Magento\Directory\Model\RegionFactory');
    $regionModel = $_regionFactory->create();

    if (is_numeric($regionId)) {
        $shipperRegion = $this->_regionFactory->create()->load($regionId);
        if($shipperRegion->getId()){
            $shipperRegionCode = $shipperRegion->getCode();
            print_r($shipperRegionCode>getData());
    }
}

Note:Direct use of object manger is not good Idea.

If you going to add try this on block class,Helper,Model,ResourceModel class Then My suggestion is that

Inject the factory class at __construct function then get region data by dependency inject.

protected $_regionFactory;
protected $_scopeConfig;
public function  __construct(
  ...
  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  \Magento\Directory\Model\RegionFactory $regionFactory,
  ...
) {
  $this->_scopeConfig = $scopeConfig;
  $this->_regionFactory = $regionFactory;
  ...
}

public function getShippingregion()
{
      $shipperRegionId = (int) $this->_scopeConfig->getValue('shipping/origin/region_id',
          ScopeInterface::SCOPE_STORE,
          [StoreId]
      );

      if (is_numeric($shipperRegionId)) {
          $shipperRegion = $this->_regionFactory->create()->load($shipperRegionId );
          $shipperRegionCode =$shipperRegion->getCode();
      }
}
1
  • 1
    Whatever you have give in the second solution is what I had, but mine was not working as I was using loadByCode(). But the creat()->load() method you have shown here is working but its deprecated. Commented Oct 3, 2017 at 23:43
1

Try below code:

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$region = $objectManager->create('Magento\Directory\Model\Region')->load(12); // Region Id
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.

1
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 state ID.
             * @param $state - numbers state ID
             *
             * @return array [region_id],[country_id],[code],[default_name],[name]
             */

            public function getRegionDataByName($state, $countryId = 'US') {
                try {
                    $region   = $this->_regionFactory->create();
                    $regionData = $region->loadByName($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->getRegionDataByName($address->getRegion())->getCode();
?>

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.