12
12
use Magento \Eav \Model \Entity \Attribute \AbstractAttribute ;
13
13
use Magento \Store \Model \Store ;
14
14
use Magento \ImportExport \Model \Import ;
15
+ use Magento \CustomerImportExport \Model \ResourceModel \Import \Address \Storage as AddressStorage ;
15
16
16
17
/**
17
18
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -100,6 +101,8 @@ class Address extends AbstractCustomer
100
101
* )
101
102
*
102
103
* @var array
104
+ * @deprected
105
+ * @see $addressStorage
103
106
*/
104
107
protected $ _addresses = [];
105
108
@@ -256,6 +259,11 @@ class Address extends AbstractCustomer
256
259
*/
257
260
private $ optionsByWebsite = [];
258
261
262
+ /**
263
+ * @var AddressStorage
264
+ */
265
+ private $ addressStorage ;
266
+
259
267
/**
260
268
* @param \Magento\Framework\Stdlib\StringUtils $string
261
269
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
@@ -276,6 +284,7 @@ class Address extends AbstractCustomer
276
284
* @param \Magento\Customer\Model\Address\Validator\Postcode $postcodeValidator
277
285
* @param array $data
278
286
* @param Sources\CountryWithWebsites|null $countryWithWebsites
287
+ * @param AddressStorage|null $addressStorage
279
288
*
280
289
* @SuppressWarnings(PHPMD.NPathComplexity)
281
290
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -299,7 +308,8 @@ public function __construct(
299
308
DateTime $ dateTime ,
300
309
\Magento \Customer \Model \Address \Validator \Postcode $ postcodeValidator ,
301
310
array $ data = [],
302
- Sources \CountryWithWebsites $ countryWithWebsites = null
311
+ Sources \CountryWithWebsites $ countryWithWebsites = null ,
312
+ AddressStorage $ addressStorage = null
303
313
) {
304
314
$ this ->_customerFactory = $ customerFactory ;
305
315
$ this ->_addressFactory = $ addressFactory ;
@@ -352,9 +362,11 @@ public function __construct(
352
362
self ::ERROR_DUPLICATE_PK ,
353
363
__ ('We found another row with this email, website and address ID combination. ' )
354
364
);
365
+ $ this ->addressStorage = $ addressStorage
366
+ ?: ObjectManager::getInstance ()->get (AddressStorage::class);
355
367
356
368
$ this ->_initAttributes ();
357
- $ this ->_initAddresses ()-> _initCountryRegions ();
369
+ $ this ->_initCountryRegions ();
358
370
}
359
371
360
372
/**
@@ -455,6 +467,8 @@ protected function _getNextEntityId()
455
467
* Initialize existent addresses data
456
468
*
457
469
* @return $this
470
+ * @deprecated
471
+ * @see prepareCustomerData
458
472
*/
459
473
protected function _initAddresses ()
460
474
{
@@ -472,6 +486,57 @@ protected function _initAddresses()
472
486
return $ this ;
473
487
}
474
488
489
+ /**
490
+ * Pre-loading customers for existing customers checks in order
491
+ * to perform mass validation/import efficiently.
492
+ * Also loading existing addresses for requested customers.
493
+ *
494
+ * @param \Traversable $rows Each row must contain data from columns email
495
+ * and website code.
496
+ *
497
+ * @return void
498
+ */
499
+ public function prepareCustomerData (\Traversable $ rows )
500
+ {
501
+ $ customersPresent = [];
502
+ foreach ($ rows as $ rowData ) {
503
+ $ email = isset ($ rowData [static ::COLUMN_EMAIL ])
504
+ ? $ rowData [static ::COLUMN_EMAIL ] : null ;
505
+ $ websiteId = isset ($ rowData [static ::COLUMN_WEBSITE ])
506
+ ? $ this ->getWebsiteId ($ rowData [static ::COLUMN_WEBSITE ]) : false ;
507
+ if ($ email && $ websiteId !== false ) {
508
+ $ customersPresent [] = [
509
+ 'email ' => $ email ,
510
+ 'website_id ' => $ websiteId
511
+ ];
512
+ }
513
+ }
514
+ $ this ->getCustomerStorage ()->prepareCustomers ($ customersPresent );
515
+
516
+ $ ids = [];
517
+ foreach ($ customersPresent as $ customerData ) {
518
+ $ id = $ this ->getCustomerStorage ()->getCustomerId (
519
+ $ customerData ['email ' ],
520
+ $ customerData ['website_id ' ]
521
+ );
522
+ if ($ id ) {
523
+ $ ids [] = $ id ;
524
+ }
525
+ }
526
+
527
+ $ this ->addressStorage ->prepareAddresses ($ ids );
528
+ }
529
+
530
+ /**
531
+ * @inheritDoc
532
+ */
533
+ public function validateData ()
534
+ {
535
+ $ this ->prepareCustomerData ($ this ->getSource ());
536
+
537
+ return parent ::validateData ();
538
+ }
539
+
475
540
/**
476
541
* Initialize country regions hash for clever recognition
477
542
*
@@ -500,6 +565,16 @@ protected function _initCountryRegions()
500
565
*/
501
566
protected function _importData ()
502
567
{
568
+ //Preparing data for mass validation/import.
569
+ $ rows = [];
570
+ while ($ bunch = $ this ->_dataSourceModel ->getNextBunch ()) {
571
+ $ rows = array_merge ($ rows , $ bunch );
572
+ }
573
+ $ this ->prepareCustomerData (new \ArrayObject ($ rows ));
574
+ unset($ bunch , $ rows );
575
+ $ this ->_dataSourceModel ->getIterator ()->rewind ();
576
+
577
+ //Importing
503
578
while ($ bunch = $ this ->_dataSourceModel ->getNextBunch ()) {
504
579
$ newRows = [];
505
580
$ updateRows = [];
@@ -588,9 +663,10 @@ protected function _prepareDataForUpdate(array $rowData)
588
663
$ defaults = [];
589
664
$ newAddress = true ;
590
665
// get address id
591
- if (isset ($ this ->_addresses [$ customerId ])
592
- && in_array ($ rowData [self ::COLUMN_ADDRESS_ID ], $ this ->_addresses [$ customerId ])
593
- ) {
666
+ if ($ this ->addressStorage ->doesExist (
667
+ $ rowData [self ::COLUMN_ADDRESS_ID ],
668
+ $ customerId
669
+ )) {
594
670
$ newAddress = false ;
595
671
$ addressId = $ rowData [self ::COLUMN_ADDRESS_ID ];
596
672
} else {
@@ -845,12 +921,11 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
845
921
$ rowNumber ,
846
922
$ multiSeparator
847
923
);
848
- } elseif ($ attributeParams ['is_required ' ] && (!isset (
849
- $ this ->_addresses [$ customerId ]
850
- ) || !in_array (
851
- $ addressId ,
852
- $ this ->_addresses [$ customerId ]
853
- ))
924
+ } elseif ($ attributeParams ['is_required ' ]
925
+ && !$ this ->addressStorage ->doesExist (
926
+ $ addressId ,
927
+ $ customerId
928
+ )
854
929
) {
855
930
$ this ->addRowError (self ::ERROR_VALUE_IS_REQUIRED , $ rowNumber , $ attributeCode );
856
931
}
@@ -906,7 +981,10 @@ protected function _validateRowForDelete(array $rowData, $rowNumber)
906
981
} else {
907
982
if (!strlen ($ addressId )) {
908
983
$ this ->addRowError (self ::ERROR_ADDRESS_ID_IS_EMPTY , $ rowNumber );
909
- } elseif (!in_array ($ addressId , $ this ->_addresses [$ customerId ])) {
984
+ } elseif (!$ this ->addressStorage ->doesExist (
985
+ $ addressId ,
986
+ $ customerId
987
+ )) {
910
988
$ this ->addRowError (self ::ERROR_ADDRESS_NOT_FOUND , $ rowNumber );
911
989
}
912
990
}
@@ -922,7 +1000,7 @@ protected function _validateRowForDelete(array $rowData, $rowNumber)
922
1000
*/
923
1001
protected function _checkRowDuplicate ($ customerId , $ addressId )
924
1002
{
925
- if (isset ( $ this ->_addresses [ $ customerId ]) && in_array ($ addressId , $ this -> _addresses [ $ customerId] )) {
1003
+ if ($ this ->addressStorage -> doesExist ($ addressId , $ customerId )) {
926
1004
if (!isset ($ this ->_importedRowPks [$ customerId ][$ addressId ])) {
927
1005
$ this ->_importedRowPks [$ customerId ][$ addressId ] = true ;
928
1006
return false ;
0 commit comments