@@ -313,23 +313,6 @@ protected function _getCustomerEntity()
313
313
return $ this ->_customerEntity ;
314
314
}
315
315
316
- /**
317
- * Get region parameters
318
- *
319
- * @return array
320
- */
321
- protected function _getRegionParameters ()
322
- {
323
- if (!$ this ->_regionParameters ) {
324
- $ this ->_regionParameters = [];
325
- /** @var $regionIdAttribute \Magento\Customer\Model\Attribute */
326
- $ regionIdAttribute = $ this ->_eavConfig ->getAttribute ($ this ->getEntityTypeCode (), 'region_id ' );
327
- $ this ->_regionParameters ['table ' ] = $ regionIdAttribute ->getBackend ()->getTable ();
328
- $ this ->_regionParameters ['attribute_id ' ] = $ regionIdAttribute ->getId ();
329
- }
330
- return $ this ->_regionParameters ;
331
- }
332
-
333
316
/**
334
317
* Get next address entity ID
335
318
*
@@ -395,7 +378,8 @@ protected function _initCountryRegions()
395
378
protected function _importData ()
396
379
{
397
380
while ($ bunch = $ this ->_dataSourceModel ->getNextBunch ()) {
398
- $ addUpdateRows = [];
381
+ $ newRows = [];
382
+ $ updateRows = [];
399
383
$ attributes = [];
400
384
$ defaults = [];
401
385
// customer default addresses (billing/shipping) data
@@ -409,7 +393,12 @@ protected function _importData()
409
393
410
394
if ($ this ->getBehavior ($ rowData ) == \Magento \ImportExport \Model \Import::BEHAVIOR_ADD_UPDATE ) {
411
395
$ addUpdateResult = $ this ->_prepareDataForUpdate ($ rowData );
412
- $ addUpdateRows [] = $ addUpdateResult ['entity_row ' ];
396
+ if ($ addUpdateResult ['entity_row_new ' ]) {
397
+ $ newRows [] = $ addUpdateResult ['entity_row_new ' ];
398
+ }
399
+ if ($ addUpdateResult ['entity_row_update ' ]) {
400
+ $ updateRows [] = $ addUpdateResult ['entity_row_update ' ];
401
+ }
413
402
$ attributes = $ this ->_mergeEntityAttributes ($ addUpdateResult ['attributes ' ], $ attributes );
414
403
$ defaults = $ this ->_mergeEntityAttributes ($ addUpdateResult ['defaults ' ], $ defaults );
415
404
} elseif ($ this ->getBehavior ($ rowData ) == \Magento \ImportExport \Model \Import::BEHAVIOR_DELETE ) {
@@ -418,7 +407,8 @@ protected function _importData()
418
407
}
419
408
420
409
$ this ->_saveAddressEntities (
421
- $ addUpdateRows
410
+ $ newRows ,
411
+ $ updateRows
422
412
)->_saveAddressAttributes (
423
413
$ attributes
424
414
)->_saveCustomerDefaults (
@@ -462,26 +452,15 @@ protected function _prepareDataForUpdate(array $rowData)
462
452
$ email = strtolower ($ rowData [self ::COLUMN_EMAIL ]);
463
453
$ customerId = $ this ->_getCustomerId ($ email , $ rowData [self ::COLUMN_WEBSITE ]);
464
454
465
- $ regionParameters = $ this ->_getRegionParameters ();
466
- $ regionIdTable = $ regionParameters ['table ' ];
467
- $ regionIdAttributeId = $ regionParameters ['attribute_id ' ];
468
-
469
- // get address attributes
470
- $ addressAttributes = [];
471
- foreach ($ this ->_attributes as $ attributeAlias => $ attributeParams ) {
472
- if (isset ($ rowData [$ attributeAlias ]) && strlen ($ rowData [$ attributeAlias ])) {
473
- if ('select ' == $ attributeParams ['type ' ]) {
474
- $ value = $ attributeParams ['options ' ][strtolower ($ rowData [$ attributeAlias ])];
475
- } elseif ('datetime ' == $ attributeParams ['type ' ]) {
476
- $ value = (new \DateTime ())->setTimestamp (strtotime ($ rowData [$ attributeAlias ]));
477
- $ value = $ value ->format (\Magento \Framework \Stdlib \DateTime::DATETIME_PHP_FORMAT );
478
- } else {
479
- $ value = $ rowData [$ attributeAlias ];
480
- }
481
- $ addressAttributes [$ attributeParams ['id ' ]] = $ value ;
482
- }
483
- }
455
+ // entity table data
456
+ $ entityRowNew = [];
457
+ $ entityRowUpdate = [];
458
+ // attribute values
459
+ $ attributes = [];
460
+ // customer default addresses
461
+ $ defaults = [];
484
462
463
+ $ newAddress = true ;
485
464
// get address id
486
465
if (isset (
487
466
$ this ->_addresses [$ customerId ]
@@ -490,30 +469,44 @@ protected function _prepareDataForUpdate(array $rowData)
490
469
$ this ->_addresses [$ customerId ]
491
470
)
492
471
) {
472
+ $ newAddress = false ;
493
473
$ addressId = $ rowData [self ::COLUMN_ADDRESS_ID ];
494
474
} else {
495
475
$ addressId = $ this ->_getNextEntityId ();
496
476
}
497
-
498
- // entity table data
499
477
$ entityRow = [
500
478
'entity_id ' => $ addressId ,
501
479
'parent_id ' => $ customerId ,
502
- 'created_at ' => (new \DateTime ())->format (\Magento \Framework \Stdlib \DateTime::DATETIME_PHP_FORMAT ),
503
480
'updated_at ' => (new \DateTime ())->format (\Magento \Framework \Stdlib \DateTime::DATETIME_PHP_FORMAT ),
504
481
];
505
482
506
- // attribute values
507
- $ attributes = [];
508
- foreach ($ this ->_attributes as $ attributeParams ) {
509
- if (isset ($ addressAttributes [$ attributeParams ['id ' ]])) {
510
- $ attributes [$ attributeParams ['table ' ]][$ addressId ][$ attributeParams ['id ' ]]
511
- = $ addressAttributes [$ attributeParams ['id ' ]];
483
+ foreach ($ this ->_attributes as $ attributeAlias => $ attributeParams ) {
484
+ if (array_key_exists ($ attributeAlias , $ rowData )) {
485
+ if (!strlen ($ rowData [$ attributeAlias ])) {
486
+ if ($ newAddress ) {
487
+ $ value = null ;
488
+ } else {
489
+ continue ;
490
+ }
491
+ } elseif ($ newAddress && !strlen ($ rowData [$ attributeAlias ])) {
492
+
493
+ } elseif ('select ' == $ attributeParams ['type ' ]) {
494
+ $ value = $ attributeParams ['options ' ][strtolower ($ rowData [$ attributeAlias ])];
495
+ } elseif ('datetime ' == $ attributeParams ['type ' ]) {
496
+ $ value = (new \DateTime ())->setTimestamp (strtotime ($ rowData [$ attributeAlias ]));
497
+ $ value = $ value ->format (\Magento \Framework \Stdlib \DateTime::DATETIME_PHP_FORMAT );
498
+ } else {
499
+ $ value = $ rowData [$ attributeAlias ];
500
+ }
501
+ if ($ attributeParams ['is_static ' ]) {
502
+ $ entityRow [$ attributeAlias ] = $ value ;
503
+ } else {
504
+ $ attributes [$ attributeParams ['table ' ]][$ addressId ][$ attributeParams ['id ' ]]= $ value ;
505
+ }
512
506
}
513
507
}
514
508
515
- // customer default addresses
516
- $ defaults = [];
509
+
517
510
foreach (self ::getDefaultAddressAttributeMapping () as $ columnName => $ attributeCode ) {
518
511
if (!empty ($ rowData [$ columnName ])) {
519
512
/** @var $attribute \Magento\Eav\Model\Entity\Attribute\AbstractAttribute */
@@ -529,32 +522,51 @@ protected function _prepareDataForUpdate(array $rowData)
529
522
530
523
if (isset ($ this ->_countryRegions [$ countryNormalized ][$ regionNormalized ])) {
531
524
$ regionId = $ this ->_countryRegions [$ countryNormalized ][$ regionNormalized ];
532
- $ attributes [$ regionIdTable ][$ addressId ][$ regionIdAttributeId ] = $ regionId ;
533
- $ tableName = $ this ->_attributes [self ::COLUMN_REGION ]['table ' ];
534
- $ regionColumnNameId = $ this ->_attributes [self ::COLUMN_REGION ]['id ' ];
535
- $ attributes [$ tableName ][$ addressId ][$ regionColumnNameId ] = $ this ->_regions [$ regionId ];
525
+ $ entityRow [self ::COLUMN_REGION ] = $ regionId ;
526
+ $ entityRow [self ::COLUMN_COUNTRY_ID ] = $ this ->_regions [$ regionId ];
536
527
}
537
528
}
538
529
539
- return ['entity_row ' => $ entityRow , 'attributes ' => $ attributes , 'defaults ' => $ defaults ];
530
+ if ($ newAddress ) {
531
+ $ entityRowNew = $ entityRow ;
532
+ $ entityRowNew ['created_at ' ] =
533
+ (new \DateTime ())->format (\Magento \Framework \Stdlib \DateTime::DATETIME_PHP_FORMAT );
534
+ } else {
535
+ $ entityRowUpdate = $ entityRow ;
536
+ }
537
+
538
+ return [
539
+ 'entity_row_new ' => $ entityRowNew ,
540
+ 'entity_row_update ' => $ entityRowUpdate ,
541
+ 'attributes ' => $ attributes ,
542
+ 'defaults ' => $ defaults ];
540
543
}
541
544
542
545
/**
543
546
* Update and insert data in entity table
544
547
*
545
- * @param array $entityRows Rows for insert
548
+ * @param array $addRows Rows for insert
549
+ * @param array $updateRows Rows for update
546
550
* @return $this
547
551
*/
548
- protected function _saveAddressEntities (array $ entityRows )
552
+ protected function _saveAddressEntities (array $ addRows , array $ updateRows )
549
553
{
550
- if ($ entityRows ) {
551
- $ this ->_connection ->insertOnDuplicate ($ this ->_entityTable , $ entityRows , ['updated_at ' ]);
554
+ if ($ addRows ) {
555
+ $ this ->_connection ->insertMultiple ($ this ->_entityTable , $ addRows );
556
+ }
557
+ if ($ updateRows ) {
558
+ //list of updated fields can be different for addresses. We can not use insertOnDuplicate for whole rows.
559
+ foreach ($ updateRows as $ row ) {
560
+ $ fields = array_diff (array_keys ($ row ), ['entity_id ' , 'parent_id ' , 'created_at ' ]);
561
+ $ this ->_connection ->insertOnDuplicate ($ this ->_entityTable , $ row , $ fields );
562
+ }
563
+
552
564
}
553
565
return $ this ;
554
566
}
555
567
556
568
/**
557
- * Save customer address attributes
569
+ * Save custom customer address attributes
558
570
*
559
571
* @param array $attributesData
560
572
* @return $this
0 commit comments