Skip to content

Commit f5c8afd

Browse files
committed
Merge remote-tracking branch 'origin/MC-33730' into 2.4.1-develop-pr25
2 parents d615cdf + e7bbc36 commit f5c8afd

File tree

3 files changed

+86
-18
lines changed

3 files changed

+86
-18
lines changed

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

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class Address extends AbstractCustomer
5454

5555
/**#@-*/
5656

57+
const COLUMN_REGION_ID = 'region_id';
58+
5759
/**#@+
5860
* Particular columns that contains of customer default addresses
5961
*/
@@ -666,13 +668,17 @@ protected function _prepareDataForUpdate(array $rowData): array
666668
}
667669
}
668670
// let's try to find region ID
669-
$entityRow['region_id'] = null;
671+
$entityRow[self::COLUMN_REGION_ID] = null;
670672

671-
if (!empty($rowData[self::COLUMN_REGION])
672-
&& $this->getCountryRegionId($rowData[self::COLUMN_COUNTRY_ID], $rowData[self::COLUMN_REGION]) !== false) {
673-
$regionId = $this->getCountryRegionId($rowData[self::COLUMN_COUNTRY_ID], $rowData[self::COLUMN_REGION]);
674-
$entityRow[self::COLUMN_REGION] = $this->_regions[$regionId];
675-
$entityRow['region_id'] = $regionId;
673+
if (!empty($entityRow[self::COLUMN_REGION]) && !empty($entityRow[self::COLUMN_COUNTRY_ID])) {
674+
$entityRow[self::COLUMN_REGION_ID] = $this->getCountryRegionId(
675+
$entityRow[self::COLUMN_COUNTRY_ID],
676+
$entityRow[self::COLUMN_REGION]
677+
);
678+
// override the region name with its proper name if region ID is found
679+
$entityRow[self::COLUMN_REGION] = $entityRow[self::COLUMN_REGION_ID] !== null
680+
? $this->_regions[$entityRow[self::COLUMN_REGION_ID]]
681+
: $entityRow[self::COLUMN_REGION];
676682
}
677683
if ($newAddress) {
678684
$entityRowNew = $entityRow;
@@ -833,7 +839,7 @@ public static function getDefaultAddressAttributeMapping()
833839
* Check if address for import is empty (for customer composite mode)
834840
*
835841
* @param array $rowData
836-
* @return array
842+
* @return bool
837843
*/
838844
protected function _isOptionalAddressEmpty(array $rowData)
839845
{
@@ -914,7 +920,8 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
914920

915921
if (isset($rowData[self::COLUMN_REGION])
916922
&& !empty($rowData[self::COLUMN_REGION])
917-
&& false === $this->getCountryRegionId(
923+
&& count($this->getCountryRegions($rowData[self::COLUMN_COUNTRY_ID])) > 0
924+
&& null === $this->getCountryRegionId(
918925
$rowData[self::COLUMN_COUNTRY_ID],
919926
$rowData[self::COLUMN_REGION]
920927
)
@@ -994,18 +1001,22 @@ public function setCustomerAttributes($customerAttributes)
9941001
*
9951002
* @param string $countryId
9961003
* @param string $region
997-
* @return bool|int
1004+
* @return int|null
9981005
*/
999-
private function getCountryRegionId(string $countryId, string $region)
1006+
private function getCountryRegionId(string $countryId, string $region): ?int
10001007
{
1001-
$countryNormalized = strtolower($countryId);
1002-
$regionNormalized = strtolower($region);
1003-
1004-
if (isset($this->_countryRegions[$countryNormalized])
1005-
&& isset($this->_countryRegions[$countryNormalized][$regionNormalized])) {
1006-
return $this->_countryRegions[$countryNormalized][$regionNormalized];
1007-
}
1008+
$countryRegions = $this->getCountryRegions($countryId);
1009+
return $countryRegions[strtolower($region)] ?? null;
1010+
}
10081011

1009-
return false;
1012+
/**
1013+
* Get country regions
1014+
*
1015+
* @param string $countryId
1016+
* @return array
1017+
*/
1018+
private function getCountryRegions(string $countryId): array
1019+
{
1020+
return $this->_countryRegions[strtolower($countryId)] ?? [];
10101021
}
10111022
}

dev/tests/integration/testsuite/Magento/CustomerImportExport/Model/Import/AddressTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@
99
*/
1010
namespace Magento\CustomerImportExport\Model\Import;
1111

12+
use Magento\Customer\Api\AddressRepositoryInterface;
13+
use Magento\Customer\Api\CustomerRepositoryInterface;
14+
use Magento\Customer\Api\Data\AddressInterface;
15+
use Magento\Framework\Api\SearchCriteriaBuilder;
1216
use Magento\Framework\App\Filesystem\DirectoryList;
1317
use Magento\Framework\Filesystem;
1418
use Magento\ImportExport\Model\Import as ImportModel;
1519
use Magento\ImportExport\Model\Import\Adapter as ImportAdapter;
20+
use Magento\ImportExport\Model\Import\Source\Csv;
1621
use Magento\TestFramework\Helper\Bootstrap;
1722
use Magento\Framework\Indexer\StateInterface;
1823
use ReflectionClass;
@@ -483,4 +488,54 @@ public function testCustomerIndexer(): void
483488
$this->assertEquals(StateInterface::STATUS_VALID, $statusBeforeImport);
484489
$this->assertEquals(StateInterface::STATUS_INVALID, $statusAfterImport);
485490
}
491+
492+
/**
493+
* Test import address with region for a country that does not have regions defined
494+
*
495+
* @magentoDataFixture Magento/Customer/_files/import_export/customer_with_addresses.php
496+
*/
497+
public function testImportAddressWithOptionalRegion()
498+
{
499+
$objectManager = Bootstrap::getObjectManager();
500+
$customerRepository = $objectManager->get(CustomerRepositoryInterface::class);
501+
$customer = $customerRepository->get('BetsyParker@example.com');
502+
$file = __DIR__ . '/_files/import_uk_address.csv';
503+
$filesystem = Bootstrap::getObjectManager()->create(Filesystem::class);
504+
$directoryWrite = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
505+
$source = new Csv($file, $directoryWrite);
506+
$errors = $this->_entityAdapter
507+
->setParameters(['behavior' => ImportModel::BEHAVIOR_ADD_UPDATE])
508+
->setSource($source)
509+
->validateData();
510+
$this->assertEmpty($errors->getAllErrors(), 'Import validation failed');
511+
$this->_entityAdapter->importData();
512+
$address = $this->getAddresses(
513+
[
514+
'parent_id' => $customer->getId(),
515+
'country_id' => 'GB',
516+
]
517+
);
518+
$this->assertCount(1, $address);
519+
$this->assertNull($address[0]->getRegionId());
520+
$this->assertEquals('Liverpool', $address[0]->getRegion()->getRegion());
521+
}
522+
523+
/**
524+
* Get Addresses by filter
525+
*
526+
* @param array $filter
527+
* @return AddressInterface[]
528+
*/
529+
private function getAddresses(array $filter): array
530+
{
531+
$objectManager = Bootstrap::getObjectManager();
532+
/** @var AddressRepositoryInterface $repository */
533+
$repository = $objectManager->create(AddressRepositoryInterface::class);
534+
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
535+
$searchCriteriaBuilder = $objectManager->get(SearchCriteriaBuilder::class);
536+
foreach ($filter as $attr => $value) {
537+
$searchCriteriaBuilder->addFilter($attr, $value);
538+
}
539+
return $repository->getList($searchCriteriaBuilder->create())->getItems();
540+
}
486541
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_website,_email,_entity_id,city,company,country_id,fax,firstname,lastname,middlename,postcode,prefix,region,region_id,street,suffix,telephone,vat_id,vat_is_valid,vat_request_date,vat_request_id,vat_request_success,_address_default_billing_,_address_default_shipping_
2+
base,"BetsyParker@example.com",,Liverpool,,GB,,Mike,Miller,,"L2 2AY",,"Liverpool",0,"Moorfields, Vernon St",," +443450507080",,,,,,,

0 commit comments

Comments
 (0)