Magento 2 get region id from state code.

      No Comments on Magento 2 get region id from state code.

Magento’s region ID’s are internal primary keys on the database that don’t mean anything to the rest of the world.  two digit state codes, such as MN or AL are a universal region identifier.

I had to import customers from an external ERP into Magento 2.  There might be a way to set a region on a customer using the two digit code, I just wrote a simple way to get the region ID from the state code.

Here is the helper I wrote for this.  Pretty straight forward.

<?php

/**
 *
 * @author Kevin Miles <kevin.miles@evermore.tech>
 */

namespace Evermore\CustomerImport\Helper;

class Address extends \Magento\Framework\App\Helper\AbstractHelper
{
    protected $_regionFactory;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Directory\Model\RegionFactory $regionFactory
    ) {
        $this->_regionFactory = $regionFactory;

        parent::__construct($context);
    }

    /**
     * Gets the region ID from the two digit state code.
     * @param $state - Two letter state code
     *
     * @return int|bool
     */
    public function getRegionCode($state, $countryId = 'US') {
        try {
            $region   = $this->_regionFactory->create();
            $regionId = $region->loadByCode( $state, $countryId )->getId();
            return $regionId;
        }
        catch(\Exception $e) {
            $this->_logger->critical($e->getMessage());
            return false;
        }
    }
}

 

Here is the code that creates the customer address.

<?php

/**
 *
 * @author Kevin Miles <kevin.miles@evermore.tech>
 */

namespace Evermore\CustomerImport\Helper;

class CustomerCreate extends \Magento\Framework\App\Helper\AbstractHelper
{
    protected $_customerFactory;
    protected $_addressFactory;
    protected $_addressHelper;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Customer\Model\AddressFactory $addressFactory,
        \FEvermore\CustomerImport\Helper\Address $addressHelper
    ) {
        $this->_customerFactory = $customerFactory;
        $this->_addressFactory = $addressFactory;
        $this->_addressHelper = $addressHelper;
        parent::__construct($context);
    }

    public function createCustomer($customer = array()) {
    
        $newCustomer = $this->_customerFactory->create();
        $newCustomer->setWebsiteId(1);  //Main website.
        $newCustomer->setGroupId(4); //Group ID of your group
    
        $newCustomer->setFirstname($customer['firstname']);
        $newCustomer->setLastname($customer['lastname']);
        $newCustomer->setEmail($customer['email']);
        $newCustomer->setPassword('P@ssw0rd');
        $newCustomer->save();
    
        $address = $this->_addressFactory->create();
        $address->setCustomerId($newCustomer->getId())
                ->setFirstname($customer['firstname'])
                ->setLastname($customer['lastname'])
                ->setCountryId($customer['country'])
                ->setStreet($customer['address'])
                ->setCity($customer['city'])
                ->setRegionId($this->_addressHelper->getRegionCode($customer['state']))  //This is where i get the region id.
                ->setPostcode($customer['zip'])
                ->setTelephone($customer['phone'])
                ->setFax($customer['fax'])
                ->setSaveInAddressBook(1)
                ->setIsDefaultBilling(1)
                ->setIsDefaultShipping(1);
        $address->save();
    }
    
}

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *