6
6
namespace Magento \CustomerImportExport \Model \Import ;
7
7
8
8
use Magento \Customer \Model \ResourceModel \Address \Attribute \Source as Sources ;
9
+ use Magento \Framework \Stdlib \DateTime ;
9
10
use Magento \ImportExport \Model \Import \ErrorProcessing \ProcessingErrorAggregatorInterface ;
10
11
use Magento \Framework \App \ObjectManager ;
11
12
use Magento \Eav \Model \Entity \Attribute \AbstractAttribute ;
12
13
use Magento \Store \Model \Store ;
14
+ use Magento \ImportExport \Model \Import ;
13
15
14
16
/**
15
17
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -21,7 +23,7 @@ class Address extends AbstractCustomer
21
23
/**#@+
22
24
* Attribute collection name
23
25
*/
24
- const ATTRIBUTE_COLLECTION_NAME = ' Magento\Customer\Model\ResourceModel\Address\Attribute\Collection ' ;
26
+ const ATTRIBUTE_COLLECTION_NAME = \ Magento \Customer \Model \ResourceModel \Address \Attribute \Collection::class ;
25
27
26
28
/**#@-*/
27
29
@@ -217,7 +219,7 @@ class Address extends AbstractCustomer
217
219
protected $ _addressFactory ;
218
220
219
221
/**
220
- * @var \Magento\Framework\Stdlib\ DateTime
222
+ * @var DateTime
221
223
*/
222
224
protected $ dateTime ;
223
225
@@ -270,7 +272,7 @@ class Address extends AbstractCustomer
270
272
* @param \Magento\Customer\Model\CustomerFactory $customerFactory
271
273
* @param \Magento\Customer\Model\ResourceModel\Address\CollectionFactory $addressColFactory
272
274
* @param \Magento\Customer\Model\ResourceModel\Address\Attribute\CollectionFactory $attributesFactory
273
- * @param \Magento\Framework\Stdlib\ DateTime $dateTime
275
+ * @param DateTime $dateTime
274
276
* @param \Magento\Customer\Model\Address\Validator\Postcode $postcodeValidator
275
277
* @param array $data
276
278
* @param Sources\CountryWithWebsites|null $countryWithWebsites
@@ -294,7 +296,7 @@ public function __construct(
294
296
\Magento \Customer \Model \CustomerFactory $ customerFactory ,
295
297
\Magento \Customer \Model \ResourceModel \Address \CollectionFactory $ addressColFactory ,
296
298
\Magento \Customer \Model \ResourceModel \Address \Attribute \CollectionFactory $ attributesFactory ,
297
- \ Magento \ Framework \ Stdlib \ DateTime $ dateTime ,
299
+ DateTime $ dateTime ,
298
300
\Magento \Customer \Model \Address \Validator \Postcode $ postcodeValidator ,
299
301
array $ data = [],
300
302
Sources \CountryWithWebsites $ countryWithWebsites = null
@@ -586,12 +588,8 @@ protected function _prepareDataForUpdate(array $rowData)
586
588
$ defaults = [];
587
589
$ newAddress = true ;
588
590
// get address id
589
- if (isset (
590
- $ this ->_addresses [$ customerId ]
591
- ) && in_array (
592
- $ rowData [self ::COLUMN_ADDRESS_ID ],
593
- $ this ->_addresses [$ customerId ]
594
- )
591
+ if (isset ($ this ->_addresses [$ customerId ])
592
+ && in_array ($ rowData [self ::COLUMN_ADDRESS_ID ], $ this ->_addresses [$ customerId ])
595
593
) {
596
594
$ newAddress = false ;
597
595
$ addressId = $ rowData [self ::COLUMN_ADDRESS_ID ];
@@ -601,7 +599,7 @@ protected function _prepareDataForUpdate(array $rowData)
601
599
$ entityRow = [
602
600
'entity_id ' => $ addressId ,
603
601
'parent_id ' => $ customerId ,
604
- 'updated_at ' => (new \DateTime ())->format (\ Magento \ Framework \ Stdlib \ DateTime::DATETIME_PHP_FORMAT ),
602
+ 'updated_at ' => (new \DateTime ())->format (DateTime::DATETIME_PHP_FORMAT ),
605
603
];
606
604
$ websiteId = $ this ->_websiteCodeToId [$ rowData [self ::COLUMN_WEBSITE ]];
607
605
@@ -624,7 +622,16 @@ protected function _prepareDataForUpdate(array $rowData)
624
622
$ value = $ attributeParams ['options ' ][strtolower ($ rowData [$ attributeAlias ])];
625
623
} elseif ('datetime ' == $ attributeParams ['type ' ]) {
626
624
$ value = (new \DateTime ())->setTimestamp (strtotime ($ rowData [$ attributeAlias ]));
627
- $ value = $ value ->format (\Magento \Framework \Stdlib \DateTime::DATETIME_PHP_FORMAT );
625
+ $ value = $ value ->format (DateTime::DATETIME_PHP_FORMAT );
626
+ } elseif ('multiselect ' == $ attributeParams ['type ' ]) {
627
+ $ separator = isset ($ this ->_parameters [Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR ]) ?
628
+ $ this ->_parameters [Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR ] :
629
+ Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR ;
630
+ $ value = str_replace (
631
+ $ separator ,
632
+ Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR ,
633
+ $ rowData [$ attributeAlias ]
634
+ );
628
635
} else {
629
636
$ value = $ rowData [$ attributeAlias ];
630
637
}
@@ -645,22 +652,11 @@ protected function _prepareDataForUpdate(array $rowData)
645
652
}
646
653
647
654
// let's try to find region ID
648
- $ entityRow ['region_id ' ] = null ;
649
- if (!empty ($ rowData [self ::COLUMN_REGION ])) {
650
- $ countryNormalized = strtolower ($ rowData [self ::COLUMN_COUNTRY_ID ]);
651
- $ regionNormalized = strtolower ($ rowData [self ::COLUMN_REGION ]);
652
-
653
- if (isset ($ this ->_countryRegions [$ countryNormalized ][$ regionNormalized ])) {
654
- $ regionId = $ this ->_countryRegions [$ countryNormalized ][$ regionNormalized ];
655
- $ entityRow [self ::COLUMN_REGION ] = $ this ->_regions [$ regionId ];
656
- $ entityRow ['region_id ' ] = $ regionId ;
657
- }
658
- }
655
+ $ entityRow = $ this ->fillRegionData ($ rowData , $ entityRow );
659
656
660
657
if ($ newAddress ) {
661
658
$ entityRowNew = $ entityRow ;
662
- $ entityRowNew ['created_at ' ] =
663
- (new \DateTime ())->format (\Magento \Framework \Stdlib \DateTime::DATETIME_PHP_FORMAT );
659
+ $ entityRowNew ['created_at ' ] = (new \DateTime ())->format (DateTime::DATETIME_PHP_FORMAT );
664
660
} else {
665
661
$ entityRowUpdate = $ entityRow ;
666
662
}
@@ -839,7 +835,16 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
839
835
continue ;
840
836
}
841
837
if (isset ($ rowData [$ attributeCode ]) && strlen ($ rowData [$ attributeCode ])) {
842
- $ this ->isAttributeValid ($ attributeCode , $ attributeParams , $ rowData , $ rowNumber );
838
+ $ multiSeparator = isset ($ this ->_parameters [Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR ]) ?
839
+ $ this ->_parameters [Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR ] :
840
+ Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR ;
841
+ $ this ->isAttributeValid (
842
+ $ attributeCode ,
843
+ $ attributeParams ,
844
+ $ rowData ,
845
+ $ rowNumber ,
846
+ $ multiSeparator
847
+ );
843
848
} elseif ($ attributeParams ['is_required ' ] && (!isset (
844
849
$ this ->_addresses [$ customerId ]
845
850
) || !in_array (
@@ -940,4 +945,28 @@ public function setCustomerAttributes($customerAttributes)
940
945
$ this ->_customerAttributes = $ customerAttributes ;
941
946
return $ this ;
942
947
}
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
+ }
943
972
}
0 commit comments