Skip to content

Commit ec026c3

Browse files
committed
ACP2E-3382: Exported customer address cannot be imported
1 parent a4cf5e6 commit ec026c3

File tree

3 files changed

+114
-6
lines changed

3 files changed

+114
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Magento\CustomerImportExport\Model\Import;
88

9-
use Magento\Customer\Model\ResourceModel\Address\Attribute\Source\CountryWithWebsites as CountryWithWebsitesSource;
9+
use Magento\CustomerImportExport\Model\Import\CountryWithWebsites as CountryWithWebsitesSource;
1010
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
1111
use Magento\Framework\App\ObjectManager;
1212
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
@@ -365,7 +365,7 @@ public function getAttributeOptions(AbstractAttribute $attribute, array $indexAt
365365
if ($attribute->getAttributeCode() === 'country_id') {
366366
//If we want to get available options for country field then we have to use alternative source
367367
// to get actual data for each website.
368-
$options = $this->countryWithWebsites->getAllOptions();
368+
$options = $this->countryWithWebsites->getCountiesPerWebsite();
369369
//Available country options now will be sorted by websites.
370370
$code = $attribute->getAttributeCode();
371371
$websiteOptions = [Store::DEFAULT_STORE_ID => $standardOptions];
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\CustomerImportExport\Model\Import;
10+
11+
use Magento\Customer\Model\Config\Share as CustomerShareConfig;
12+
use Magento\Directory\Model\AllowedCountries;
13+
use Magento\Directory\Model\ResourceModel\Country\Collection as CountryCollection;
14+
use Magento\Directory\Model\ResourceModel\Country\CollectionFactory as CountryCollectionFactory;
15+
use Magento\Store\Model\ScopeInterface;
16+
use Magento\Store\Model\StoreManagerInterface;
17+
18+
/**
19+
* Return allowed countries for specified website for customer import
20+
*/
21+
class CountryWithWebsites
22+
{
23+
/**
24+
* @var CountryCollectionFactory
25+
*/
26+
private $countriesFactory;
27+
28+
/**
29+
* @var AllowedCountries
30+
*/
31+
private $allowedCountriesReader;
32+
33+
/**
34+
* @var array
35+
*/
36+
private $allowedCountries;
37+
38+
/**
39+
* @var StoreManagerInterface
40+
*/
41+
private $storeManager;
42+
43+
/**
44+
* @var CustomerShareConfig
45+
*/
46+
private $shareConfig;
47+
48+
/**
49+
* @param CountryCollectionFactory $countriesFactory
50+
* @param AllowedCountries $allowedCountriesReader
51+
* @param StoreManagerInterface $storeManager
52+
* @param CustomerShareConfig $shareConfig
53+
*/
54+
public function __construct(
55+
CountryCollectionFactory $countriesFactory,
56+
AllowedCountries $allowedCountriesReader,
57+
StoreManagerInterface $storeManager,
58+
CustomerShareConfig $shareConfig
59+
) {
60+
$this->countriesFactory = $countriesFactory;
61+
$this->allowedCountriesReader = $allowedCountriesReader;
62+
$this->storeManager = $storeManager;
63+
$this->shareConfig = $shareConfig;
64+
}
65+
66+
/**
67+
* Get allowed countries for specified website
68+
*
69+
* @return array
70+
*/
71+
public function getCountiesPerWebsite(): array
72+
{
73+
if (!$this->allowedCountries) {
74+
$websiteIds = [];
75+
76+
foreach ($this->storeManager->getWebsites() as $website) {
77+
$countries = $this->allowedCountriesReader
78+
->getAllowedCountries(ScopeInterface::SCOPE_WEBSITE, $website->getId());
79+
$allowedCountries[] = $countries;
80+
81+
foreach ($countries as $countryCode) {
82+
$websiteIds[$countryCode][] = $website->getId();
83+
}
84+
}
85+
86+
$this->allowedCountries = $this->createCountriesCollection()
87+
->addFieldToFilter('country_id', ['in' => $allowedCountries])
88+
->toOptionArray();
89+
90+
foreach ($this->allowedCountries as &$option) {
91+
if (isset($websiteIds[$option['value']])) {
92+
$option['website_ids'] = $websiteIds[$option['value']];
93+
}
94+
}
95+
}
96+
97+
return $this->allowedCountries;
98+
}
99+
100+
/**
101+
* Create Countries Collection with all countries
102+
*
103+
* @return CountryCollection
104+
*/
105+
private function createCountriesCollection()
106+
{
107+
return $this->countriesFactory->create();
108+
}
109+
}

app/code/Magento/CustomerImportExport/Test/Unit/Model/Import/AddressTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Magento\Customer\Model\CustomerFactory;
1313
use Magento\Customer\Model\Indexer\Processor;
1414
use Magento\Customer\Model\ResourceModel\Address\Attribute as AddressAttribute;
15-
use Magento\Customer\Model\ResourceModel\Address\Attribute\Source\CountryWithWebsites;
15+
use Magento\CustomerImportExport\Model\Import\CountryWithWebsites;
1616
use Magento\CustomerImportExport\Model\Import\Address;
1717
use Magento\CustomerImportExport\Model\ResourceModel\Import\Customer\Storage;
1818
use Magento\CustomerImportExport\Model\ResourceModel\Import\Customer\StorageFactory;
@@ -145,7 +145,7 @@ class AddressTest extends TestCase
145145
protected $errorAggregator;
146146

147147
/**
148-
* @var AddressAttribute\Source\CountryWithWebsites|MockObject
148+
* @var CountryWithWebsites|MockObject
149149
*/
150150
private $countryWithWebsites;
151151

@@ -168,8 +168,7 @@ protected function setUp(): void
168168
->disableOriginalConstructor()
169169
->getMock();
170170
$this->countryWithWebsites
171-
172-
->method('getAllOptions')
171+
->method('getCountiesPerWebsite')
173172
->willReturn([]);
174173
$this->_model = $this->_getModelMock();
175174
$this->errorAggregator = $this->createPartialMock(

0 commit comments

Comments
 (0)