Skip to content

Commit 863c05a

Browse files
author
Sergii Kovalenko
committed
MAGETWO-56941: [Github] Allowed countries for customer address in admin using storeview configuration #2946
1 parent fe86a0a commit 863c05a

File tree

6 files changed

+31
-16
lines changed

6 files changed

+31
-16
lines changed

app/code/Magento/Customer/Model/Plugin/AllowedCountries.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function __construct(
4444
* @param \Magento\Directory\Model\AllowedCountries $subject
4545
* @param string | null $filter
4646
* @param string $scope
47+
* @return array
48+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
4749
*/
4850
public function beforeGetAllowedCountries(
4951
\Magento\Directory\Model\AllowedCountries $subject,

app/code/Magento/Customer/Model/ResourceModel/Address/Attribute/Source/CountryWithWebsites.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ class CountryWithWebsites extends \Magento\Eav\Model\Entity\Attribute\Source\Tab
4343
private $shareConfig;
4444

4545
/**
46+
* CountryWithWebsites constructor.
4647
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
4748
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory
4849
* @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countriesFactory
4950
* @param AllowedCountries $allowedCountriesReader
5051
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
52+
* @param Share $shareConfig
5153
*/
5254
public function __construct(
5355
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory,
@@ -89,7 +91,6 @@ public function getAllOptions()
8991
$allowedCountries = $this->allowedCountriesReader->getAllowedCountries();
9092
}
9193

92-
9394
$this->options = $this->createCountriesCollection()
9495
->addFieldToFilter('country_id', ['in' => $allowedCountries])
9596
->toOptionArray();

app/code/Magento/Customer/Setup/UpgradeData.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public function __construct(
6060
CustomerSetupFactory $customerSetupFactory,
6161
IndexerRegistry $indexerRegistry,
6262
\Magento\Eav\Model\Config $eavConfig
63-
) {
63+
)
64+
{
6465
$this->customerSetupFactory = $customerSetupFactory;
6566
$this->indexerRegistry = $indexerRegistry;
6667
$this->eavConfig = $eavConfig;
@@ -189,6 +190,19 @@ private function mergeAllowedCountries(array $countries, array $newCountries, $i
189190
return $countries;
190191
}
191192

193+
/**
194+
* Retrieve countries not depending on global scope
195+
*
196+
* @param string $scope
197+
* @param int $scopeCode
198+
* @return array
199+
*/
200+
private function getAllowedCountries($scope, $scopeCode)
201+
{
202+
$reader = $this->getAllowedCountriesReader();
203+
return $reader->makeCountriesUnique($reader->getCountriesFromConfig($scope, $scopeCode));
204+
}
205+
192206
/**
193207
* Merge allowed countries from stores to websites
194208
*
@@ -202,17 +216,15 @@ private function migrateStoresAllowedCountriesToWebsite(SetupInterface $setup)
202216
foreach ($this->getStoreManager()->getStores() as $store) {
203217
$allowedCountries = $this->mergeAllowedCountries(
204218
$allowedCountries,
205-
$this->getAllowedCountriesReader()
206-
->getAllowedCountries(ScopeInterface::SCOPE_STORE, $store->getId()),
219+
$this->getAllowedCountries(ScopeInterface::SCOPE_STORE, $store->getId()),
207220
$store->getWebsiteId()
208221
);
209222
}
210223
//Process stores
211224
foreach ($this->getStoreManager()->getWebsites() as $website) {
212225
$allowedCountries = $this->mergeAllowedCountries(
213226
$allowedCountries,
214-
$this->getAllowedCountriesReader()
215-
->getAllowedCountries(ScopeInterface::SCOPE_WEBSITE, $website->getId()),
227+
$this->getAllowedCountries(ScopeInterface::SCOPE_WEBSITE, $website->getId()),
216228
$website->getId()
217229
);
218230
}

app/code/Magento/Customer/Test/Unit/Model/Plugin/AllowedCountriesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public function testGetAllowedCountriesWithGlobalScope()
5656
->willReturn([$websiteMock]);
5757

5858
$this->assertEquals(
59-
[[$expectedFilter], $expectedScope],
59+
[$expectedScope, [$expectedFilter]],
6060
$this->plugin->beforeGetAllowedCountries($originalAllowedCountriesMock)
6161
);
6262
}
63-
}
63+
}

app/code/Magento/Customer/Test/Unit/Model/ResourceModel/Address/Attribute/Source/CountryWithWebsitesTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ public function testGetAllOptions()
9393
$this->allowedCountriesMock->expects($this->exactly(2))
9494
->method('getAllowedCountries')
9595
->withConsecutive(
96-
[1, 'website'],
97-
[2, 'website']
96+
['website', 1],
97+
['website', 2]
9898
)
9999
->willReturnMap([
100-
[1, 'website', ['AM' => 'AM']],
101-
[2, 'website', ['AM' => 'AM', 'DZ' => 'DZ']]
100+
['website', 1, ['AM' => 'AM']],
101+
['website', 2, ['AM' => 'AM', 'DZ' => 'DZ']]
102102
]);
103103
$this->countriesFactoryMock->expects($this->once())
104104
->method('create')

app/code/Magento/Directory/Model/AllowedCountries.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ public function getAllowedCountries(
7171
$allowedCountries = $this->getCountriesFromConfig($scope, $scopeCode);
7272
}
7373

74-
return $this->getUniqueCountries($allowedCountries);
74+
return $this->makeCountriesUnique($allowedCountries);
7575
}
7676

7777
/**
7878
* Resolve scope code by scope
7979
*
8080
* @throws \InvalidArgumentException
81-
* @param $scope
81+
* @param string $scope
8282
* @return array|int
8383
*/
8484
private function getDefaultScopeCode($scope)
@@ -105,7 +105,7 @@ private function getDefaultScopeCode($scope)
105105
* @param array $allowedCountries
106106
* @return array
107107
*/
108-
private function getUniqueCountries(array $allowedCountries)
108+
public function makeCountriesUnique(array $allowedCountries)
109109
{
110110
return array_combine($allowedCountries, $allowedCountries);
111111
}
@@ -117,7 +117,7 @@ private function getUniqueCountries(array $allowedCountries)
117117
* @param int $scopeCode
118118
* @return array
119119
*/
120-
private function getCountriesFromConfig($scope, $scopeCode)
120+
public function getCountriesFromConfig($scope, $scopeCode)
121121
{
122122
return explode(
123123
',',

0 commit comments

Comments
 (0)