/** * Adopt specified address object to be compatible with Magento * * @param \Magento\Framework\DataObject $address * @return void */ protected function _applyStreetAndRegionWorkarounds(\Magento\Framework\DataObject $address) { // merge street addresses into 1 if ($address->hasStreet2()) { $address->setStreet(implode("\n", [$address->getStreet(), $address->getStreetLine(2)])); $address->unsStreet2(); } // attempt to fetch region_id from directory if ($address->getCountryId() && $address->getRegion()) { $regions = $this->_countryFactory->create()->loadByCode($address->getCountryId())->getRegionCollection()->addRegionCodeOrNameFilter($address->getRegion())->setPageSize(1); foreach ($regions as $region) { $address->setRegionId($region->getId()); $address->setExportedKeys(array_merge($address->getExportedKeys(), ['region_id'])); break; } } }