0

I am passing the parameters as

'recipientstate' => $customer['region'] and

'cardholderstate' => $customerbilling['region']

Right now I am getting the region value's as ARIZONA , but I want it as AZ in Magento 2.1.7

How to get the solution for this.. I googled a lot but not able to find perfect answer

Any help would be appreciated.

8
  • cod you update you code here , it will be easier to help Commented Mar 5, 2019 at 7:40
  • I have created an API in success.phtml file, so its very confidential, can't share the code..
    – Amy
    Commented Mar 5, 2019 at 12:00
  • are you getting region data using api ? Commented Mar 5, 2019 at 12:25
  • No, I have not used api..
    – Amy
    Commented Mar 5, 2019 at 12:32
  • how are you using code to get region code, ? so i know what part is not working and what is error Commented Mar 5, 2019 at 12:42

2 Answers 2

0

You can get Region data by region name defined in module-directory\Model\Region.php:

/**
 * Load region by name
 *
 * @param string $name
 * @param string $countryId
 * @return $this
 */
public function loadByName($name, $countryId)
{
    $this->_getResource()->loadByName($this, $name, $countryId);
    return $this;
}
1
  • It's not working..
    – Amy
    Commented Mar 5, 2019 at 12:00
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();
    //see array incomment above to get other data
?>

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.