Skip to content

Commit b01dbd9

Browse files
committed
MAGETWO-69857: Issue with importing customer address
1 parent 7723dff commit b01dbd9

File tree

1 file changed

+34
-24
lines changed
  • app/code/Magento/CustomerImportExport/Model/Import

1 file changed

+34
-24
lines changed

app/code/Magento/CustomerImportExport/Model/Import/Address.php

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\CustomerImportExport\Model\Import;
77

88
use Magento\Customer\Model\ResourceModel\Address\Attribute\Source as Sources;
9+
use Magento\Framework\Stdlib\DateTime;
910
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
1011
use Magento\Framework\App\ObjectManager;
1112
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
@@ -218,7 +219,7 @@ class Address extends AbstractCustomer
218219
protected $_addressFactory;
219220

220221
/**
221-
* @var \Magento\Framework\Stdlib\DateTime
222+
* @var DateTime
222223
*/
223224
protected $dateTime;
224225

@@ -271,7 +272,7 @@ class Address extends AbstractCustomer
271272
* @param \Magento\Customer\Model\CustomerFactory $customerFactory
272273
* @param \Magento\Customer\Model\ResourceModel\Address\CollectionFactory $addressColFactory
273274
* @param \Magento\Customer\Model\ResourceModel\Address\Attribute\CollectionFactory $attributesFactory
274-
* @param \Magento\Framework\Stdlib\DateTime $dateTime
275+
* @param DateTime $dateTime
275276
* @param \Magento\Customer\Model\Address\Validator\Postcode $postcodeValidator
276277
* @param array $data
277278
* @param Sources\CountryWithWebsites|null $countryWithWebsites
@@ -295,7 +296,7 @@ public function __construct(
295296
\Magento\Customer\Model\CustomerFactory $customerFactory,
296297
\Magento\Customer\Model\ResourceModel\Address\CollectionFactory $addressColFactory,
297298
\Magento\Customer\Model\ResourceModel\Address\Attribute\CollectionFactory $attributesFactory,
298-
\Magento\Framework\Stdlib\DateTime $dateTime,
299+
DateTime $dateTime,
299300
\Magento\Customer\Model\Address\Validator\Postcode $postcodeValidator,
300301
array $data = [],
301302
Sources\CountryWithWebsites $countryWithWebsites = null
@@ -587,12 +588,8 @@ protected function _prepareDataForUpdate(array $rowData)
587588
$defaults = [];
588589
$newAddress = true;
589590
// get address id
590-
if (isset(
591-
$this->_addresses[$customerId]
592-
) && in_array(
593-
$rowData[self::COLUMN_ADDRESS_ID],
594-
$this->_addresses[$customerId]
595-
)
591+
if (isset($this->_addresses[$customerId])
592+
&& in_array($rowData[self::COLUMN_ADDRESS_ID], $this->_addresses[$customerId])
596593
) {
597594
$newAddress = false;
598595
$addressId = $rowData[self::COLUMN_ADDRESS_ID];
@@ -602,7 +599,7 @@ protected function _prepareDataForUpdate(array $rowData)
602599
$entityRow = [
603600
'entity_id' => $addressId,
604601
'parent_id' => $customerId,
605-
'updated_at' => (new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT),
602+
'updated_at' => (new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT),
606603
];
607604
$websiteId = $this->_websiteCodeToId[$rowData[self::COLUMN_WEBSITE]];
608605

@@ -625,7 +622,7 @@ protected function _prepareDataForUpdate(array $rowData)
625622
$value = $attributeParams['options'][strtolower($rowData[$attributeAlias])];
626623
} elseif ('datetime' == $attributeParams['type']) {
627624
$value = (new \DateTime())->setTimestamp(strtotime($rowData[$attributeAlias]));
628-
$value = $value->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
625+
$value = $value->format(DateTime::DATETIME_PHP_FORMAT);
629626
} elseif ('multiselect' == $attributeParams['type']) {
630627
$separator = isset($this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR]) ?
631628
$this->_parameters[Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR] :
@@ -655,22 +652,11 @@ protected function _prepareDataForUpdate(array $rowData)
655652
}
656653

657654
// let's try to find region ID
658-
$entityRow['region_id'] = null;
659-
if (!empty($rowData[self::COLUMN_REGION])) {
660-
$countryNormalized = strtolower($rowData[self::COLUMN_COUNTRY_ID]);
661-
$regionNormalized = strtolower($rowData[self::COLUMN_REGION]);
662-
663-
if (isset($this->_countryRegions[$countryNormalized][$regionNormalized])) {
664-
$regionId = $this->_countryRegions[$countryNormalized][$regionNormalized];
665-
$entityRow[self::COLUMN_REGION] = $this->_regions[$regionId];
666-
$entityRow['region_id'] = $regionId;
667-
}
668-
}
655+
$entityRow = $this->fillRegionData($rowData, $entityRow);
669656

670657
if ($newAddress) {
671658
$entityRowNew = $entityRow;
672-
$entityRowNew['created_at'] =
673-
(new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
659+
$entityRowNew['created_at'] = (new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT);
674660
} else {
675661
$entityRowUpdate = $entityRow;
676662
}
@@ -959,4 +945,28 @@ public function setCustomerAttributes($customerAttributes)
959945
$this->_customerAttributes = $customerAttributes;
960946
return $this;
961947
}
948+
949+
/**
950+
* Try to get region_id and set it to $entityRow.
951+
*
952+
* @param array $rowData
953+
* @param array $entityRow
954+
* @return array
955+
*/
956+
private function fillRegionData(array $rowData, array $entityRow)
957+
{
958+
$entityRow['region_id'] = null;
959+
if (!empty($rowData[self::COLUMN_REGION])) {
960+
$countryNormalized = strtolower($rowData[self::COLUMN_COUNTRY_ID]);
961+
$regionNormalized = strtolower($rowData[self::COLUMN_REGION]);
962+
963+
if (isset($this->_countryRegions[$countryNormalized][$regionNormalized])) {
964+
$regionId = $this->_countryRegions[$countryNormalized][$regionNormalized];
965+
$entityRow[self::COLUMN_REGION] = $this->_regions[$regionId];
966+
$entityRow['region_id'] = $regionId;
967+
}
968+
}
969+
970+
return $entityRow;
971+
}
962972
}

0 commit comments

Comments
 (0)