{"id":292,"date":"2017-12-11T20:07:50","date_gmt":"2017-12-11T20:07:50","guid":{"rendered":"http:\/\/www.evermoretechnologies.com\/blog\/?p=292"},"modified":"2017-12-11T20:20:30","modified_gmt":"2017-12-11T20:20:30","slug":"magento-2-get-region-id-from-state-code","status":"publish","type":"post","link":"https:\/\/www.evermoretechnologies.com\/blog\/2017\/12\/magento-2-get-region-id-from-state-code\/","title":{"rendered":"Magento 2 get region id from state code."},"content":{"rendered":"<p>Magento&#8217;s region ID&#8217;s are internal primary keys on the database that don&#8217;t mean anything to the rest of the world.\u00a0 two digit state codes, such as MN or AL are a universal region identifier.<\/p>\n<p>I had to import customers from an external ERP into Magento 2.\u00a0 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.<\/p>\n<p>Here is the helper I wrote for this.\u00a0 Pretty straight forward.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?php\r\n\r\n\/**\r\n *\r\n * @author Kevin Miles &lt;kevin.miles@evermore.tech&gt;\r\n *\/\r\n\r\nnamespace Evermore\\CustomerImport\\Helper;\r\n\r\nclass Address extends \\Magento\\Framework\\App\\Helper\\AbstractHelper\r\n{\r\n    protected $_regionFactory;\r\n\r\n    public function __construct(\r\n        \\Magento\\Framework\\App\\Helper\\Context $context,\r\n        \\Magento\\Directory\\Model\\RegionFactory $regionFactory\r\n    ) {\r\n        $this-&gt;_regionFactory = $regionFactory;\r\n\r\n        parent::__construct($context);\r\n    }\r\n\r\n    \/**\r\n     * Gets the region ID from the two digit state code.\r\n     * @param $state - Two letter state code\r\n     *\r\n     * @return int|bool\r\n     *\/\r\n    public function getRegionCode($state, $countryId = 'US') {\r\n        try {\r\n            $region   = $this-&gt;_regionFactory-&gt;create();\r\n            $regionId = $region-&gt;loadByCode( $state, $countryId )-&gt;getId();\r\n            return $regionId;\r\n        }\r\n        catch(\\Exception $e) {\r\n            $this-&gt;_logger-&gt;critical($e-&gt;getMessage());\r\n            return false;\r\n        }\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Here is the code that creates the customer address.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?php\r\n\r\n\/**\r\n *\r\n * @author Kevin Miles &lt;kevin.miles@evermore.tech&gt;\r\n *\/\r\n\r\nnamespace Evermore\\CustomerImport\\Helper;\r\n\r\nclass CustomerCreate extends \\Magento\\Framework\\App\\Helper\\AbstractHelper\r\n{\r\n    protected $_customerFactory;\r\n    protected $_addressFactory;\r\n    protected $_addressHelper;\r\n\r\n    public function __construct(\r\n        \\Magento\\Framework\\App\\Helper\\Context $context,\r\n        \\Magento\\Customer\\Model\\CustomerFactory $customerFactory,\r\n        \\Magento\\Customer\\Model\\AddressFactory $addressFactory,\r\n        \\FEvermore\\CustomerImport\\Helper\\Address $addressHelper\r\n    ) {\r\n        $this-&gt;_customerFactory = $customerFactory;\r\n        $this-&gt;_addressFactory = $addressFactory;\r\n        $this-&gt;_addressHelper = $addressHelper;\r\n        parent::__construct($context);\r\n    }\r\n\r\n    public function createCustomer($customer = array()) {\r\n    \r\n        $newCustomer = $this-&gt;_customerFactory-&gt;create();\r\n        $newCustomer-&gt;setWebsiteId(1);  \/\/Main website.\r\n        $newCustomer-&gt;setGroupId(4); \/\/Group ID of your group\r\n    \r\n        $newCustomer-&gt;setFirstname($customer['firstname']);\r\n        $newCustomer-&gt;setLastname($customer['lastname']);\r\n        $newCustomer-&gt;setEmail($customer['email']);\r\n        $newCustomer-&gt;setPassword('P@ssw0rd');\r\n        $newCustomer-&gt;save();\r\n    \r\n        $address = $this-&gt;_addressFactory-&gt;create();\r\n        $address-&gt;setCustomerId($newCustomer-&gt;getId())\r\n                -&gt;setFirstname($customer['firstname'])\r\n                -&gt;setLastname($customer['lastname'])\r\n                -&gt;setCountryId($customer['country'])\r\n                -&gt;setStreet($customer['address'])\r\n                -&gt;setCity($customer['city'])\r\n                -&gt;setRegionId($this-&gt;_addressHelper-&gt;getRegionCode($customer['state']))  \/\/This is where i get the region id.\r\n                -&gt;setPostcode($customer['zip'])\r\n                -&gt;setTelephone($customer['phone'])\r\n                -&gt;setFax($customer['fax'])\r\n                -&gt;setSaveInAddressBook(1)\r\n                -&gt;setIsDefaultBilling(1)\r\n                -&gt;setIsDefaultShipping(1);\r\n        $address-&gt;save();\r\n    }\r\n    \r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento&#8217;s region ID&#8217;s are internal primary keys on the database that don&#8217;t mean anything to the rest of the world.\u00a0 two digit state codes, such as MN or AL are&#8230; <a href=\"https:\/\/www.evermoretechnologies.com\/blog\/2017\/12\/magento-2-get-region-id-from-state-code\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[34,35],"tags":[],"_links":{"self":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/292"}],"collection":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=292"}],"version-history":[{"count":1,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/292\/revisions"}],"predecessor-version":[{"id":293,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/292\/revisions\/293"}],"wp:attachment":[{"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.evermoretechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}