Skip to content

Commit cc550f5

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

File tree

13 files changed

+384
-295
lines changed

13 files changed

+384
-295
lines changed

app/code/Magento/Customer/Model/Customer/DataProvider.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
5858
*/
5959
protected $loadedData;
6060

61+
/**
62+
* @var CountryWithWebsites
63+
*/
64+
private $countryByWebsiteSource;
65+
66+
/**
67+
* @var \Magento\Customer\Model\Config\Share
68+
*/
69+
private $shareConfig;
70+
6171
/**
6272
* EAV attribute properties to fetch from meta storage
6373
* @var array
@@ -337,7 +347,11 @@ protected function getAttributesMeta(Type $entityType)
337347
*/
338348
private function getCountryByWebsiteSource()
339349
{
340-
return ObjectManager::getInstance()->get(CountryWithWebsites::class);
350+
if (!$this->countryByWebsiteSource) {
351+
$this->countryByWebsiteSource = ObjectManager::getInstance()->get(CountryWithWebsites::class);
352+
}
353+
354+
return $this->countryByWebsiteSource;
341355
}
342356

343357
/**
@@ -346,7 +360,11 @@ private function getCountryByWebsiteSource()
346360
*/
347361
private function getShareConfig()
348362
{
349-
return ObjectManager::getInstance()->get(\Magento\Customer\Model\Config\Share::class);
363+
if (!$this->shareConfig) {
364+
$this->shareConfig = ObjectManager::getInstance()->get(\Magento\Customer\Model\Config\Share::class);
365+
}
366+
367+
return $this->shareConfig;
350368
}
351369

352370
/**
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Customer\Model\Plugin;
8+
9+
use Magento\Customer\Model\Config\Share;
10+
use Magento\Store\Api\Data\WebsiteInterface;
11+
use Magento\Store\Model\ScopeInterface;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
14+
/**
15+
* Class AllowedCountries
16+
*/
17+
class AllowedCountries
18+
{
19+
/**
20+
* @var \Magento\Customer\Model\Config\Share
21+
*/
22+
private $shareConfig;
23+
24+
/**
25+
* @var StoreManagerInterface
26+
*/
27+
private $storeManager;
28+
29+
/**
30+
* @param Share $share
31+
* @param StoreManagerInterface $storeManager
32+
*/
33+
public function __construct(
34+
Share $share,
35+
StoreManagerInterface $storeManager
36+
) {
37+
$this->shareConfig = $share;
38+
$this->storeManager = $storeManager;
39+
}
40+
41+
/**
42+
* Retrieve all allowed countries or specific by scope depends on customer share setting
43+
* @param \Magento\Directory\Model\AllowedCountries $subject
44+
* @param string | null $filter
45+
* @param string $scope
46+
*/
47+
public function beforeGetAllowedCountries(
48+
\Magento\Directory\Model\AllowedCountries $subject,
49+
$filter = null,
50+
$scope = ScopeInterface::SCOPE_WEBSITE
51+
) {
52+
if ($this->shareConfig->isGlobalScope()) {
53+
//Check if we have shared accounts - than merge all website allowed countries
54+
$filter = array_map(function (WebsiteInterface $website) {
55+
return $website->getId();
56+
}, $this->storeManager->getWebsites());
57+
$scope = ScopeInterface::SCOPE_WEBSITES;
58+
}
59+
60+
return [$filter, $scope];
61+
}
62+
}

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

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@
1111
*/
1212
namespace Magento\Customer\Model\ResourceModel\Address\Attribute\Source;
1313

14-
use Magento\Customer\Api\Data\CustomerInterface;
15-
use Magento\Customer\Model\Customer;
16-
use Magento\Directory\Model\CountryHandlerInterface;
17-
use Magento\Framework\App\Config\ScopeConfigInterface;
18-
use Magento\Framework\App\ObjectManager;
14+
use Magento\Customer\Model\Config\Share;
15+
use Magento\Directory\Model\AllowedCountries;
1916
use Magento\Store\Model\ScopeInterface;
20-
use Magento\Store\Model\StoreManagerInterface;
2117

2218
class CountryWithWebsites extends \Magento\Eav\Model\Entity\Attribute\Source\Table
2319
{
@@ -27,9 +23,9 @@ class CountryWithWebsites extends \Magento\Eav\Model\Entity\Attribute\Source\Tab
2723
private $countriesFactory;
2824

2925
/**
30-
* @var \Magento\Customer\Model\CountryHandler
26+
* @var \Magento\Directory\Model\AllowedCountries
3127
*/
32-
private $countryHandler;
28+
private $allowedCountriesReader;
3329

3430
/**
3531
* @var array
@@ -42,23 +38,29 @@ class CountryWithWebsites extends \Magento\Eav\Model\Entity\Attribute\Source\Tab
4238
private $storeManager;
4339

4440
/**
45-
* CountryWithWebsites constructor.
41+
* @var Share
42+
*/
43+
private $shareConfig;
44+
45+
/**
4646
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
4747
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory
4848
* @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countriesFactory
49-
* @param \Magento\Directory\Model\CountryHandlerInterface $countryHandler
49+
* @param AllowedCountries $allowedCountries
5050
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
5151
*/
5252
public function __construct(
5353
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory,
5454
\Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory,
5555
\Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countriesFactory,
56-
\Magento\Directory\Model\CountryHandlerInterface $countryHandler,
57-
\Magento\Store\Model\StoreManagerInterface $storeManager
56+
\Magento\Directory\Model\AllowedCountries $allowedCountries,
57+
\Magento\Store\Model\StoreManagerInterface $storeManager,
58+
\Magento\Customer\Model\Config\Share $shareConfig
5859
) {
5960
$this->countriesFactory = $countriesFactory;
60-
$this->countryHandler = $countryHandler;
61+
$this->allowedCountriesReader = $allowedCountries;
6162
$this->storeManager = $storeManager;
63+
$this->shareConfig = $shareConfig;
6264
parent::__construct($attrOptionCollectionFactory, $attrOptionFactory);
6365
}
6466

@@ -73,16 +75,21 @@ public function getAllOptions()
7375
$allowedCountries = [];
7476
$websiteIds = [];
7577

76-
foreach ($this->storeManager->getWebsites() as $website) {
77-
$countries = $this->countryHandler
78-
->getAllowedCountries($website->getId(), ScopeInterface::SCOPE_WEBSITE, true);
79-
$allowedCountries = array_merge($allowedCountries, $countries);
78+
if (!$this->shareConfig->isGlobalScope()) {
79+
foreach ($this->storeManager->getWebsites() as $website) {
80+
$countries = $this->allowedCountriesReader
81+
->getAllowedCountries($website->getId(), ScopeInterface::SCOPE_WEBSITE);
82+
$allowedCountries = array_merge($allowedCountries, $countries);
8083

81-
foreach ($countries as $countryCode) {
82-
$websiteIds[$countryCode][] = $website->getId();
84+
foreach ($countries as $countryCode) {
85+
$websiteIds[$countryCode][] = $website->getId();
86+
}
8387
}
88+
} else {
89+
$allowedCountries = $this->allowedCountriesReader->getAllowedCountries();
8490
}
8591

92+
8693
$this->options = $this->createCountriesCollection()
8794
->addFieldToFilter('country_id', ['in' => $allowedCountries])
8895
->toOptionArray();
@@ -98,6 +105,7 @@ public function getAllOptions()
98105
}
99106

100107
/**
108+
* Create Countries Collection with all countries
101109
* @return \Magento\Directory\Model\ResourceModel\Country\Collection
102110
*/
103111
private function createCountriesCollection()

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
namespace Magento\Customer\Setup;
88

99
use Magento\Customer\Model\Customer;
10-
use Magento\Directory\Model\CountryHandlerInterface;
10+
use Magento\Directory\Model\AllowedCountries;
11+
use Magento\Framework\App\ObjectManager;
1112
use Magento\Framework\Encryption\Encryptor;
1213
use Magento\Framework\Indexer\IndexerRegistry;
1314
use Magento\Framework\Setup\SetupInterface;
@@ -30,6 +31,11 @@ class UpgradeData implements UpgradeDataInterface
3031
*/
3132
protected $customerSetupFactory;
3233

34+
/**
35+
* @var AllowedCountries
36+
*/
37+
private $allowedCountriesReader;
38+
3339
/**
3440
* @var IndexerRegistry
3541
*/
@@ -138,12 +144,17 @@ private function getStoreManager()
138144
}
139145

140146
/**
147+
* Retrieve Allowed Countries Reader
141148
* @deprecated
142-
* @return CountryHandlerInterface
149+
* @return AllowedCountries
143150
*/
144-
private function getCountryHandler()
151+
private function getAllowedCountriesReader()
145152
{
146-
return \Magento\Framework\App\ObjectManager::getInstance()->get(CountryHandlerInterface::class);
153+
if (!$this->allowedCountriesReader) {
154+
$this->allowedCountriesReader = ObjectManager::getInstance()->get(AllowedCountries::class);
155+
}
156+
157+
return $this->allowedCountriesReader;
147158
}
148159

149160
/**
@@ -175,15 +186,17 @@ private function migrateStoresAllowedCountriesToWebsite(SetupInterface $setup)
175186
foreach ($this->getStoreManager()->getStores() as $store) {
176187
$allowedCountries = $this->mergeAllowedCountries(
177188
$allowedCountries,
178-
$this->getCountryHandler()->getAllowedCountries($store->getId(), ScopeInterface::SCOPE_STORE),
189+
$this->getAllowedCountriesReader()
190+
->getAllowedCountries($store->getId(), ScopeInterface::SCOPE_STORE),
179191
$store->getWebsiteId()
180192
);
181193
}
182194
//Process stores
183195
foreach ($this->getStoreManager()->getWebsites() as $website) {
184196
$allowedCountries = $this->mergeAllowedCountries(
185197
$allowedCountries,
186-
$this->getCountryHandler()->getAllowedCountries($website->getId(), ScopeInterface::SCOPE_WEBSITE),
198+
$this->getAllowedCountriesReader()
199+
->getAllowedCountries($website->getId(), ScopeInterface::SCOPE_WEBSITE),
187200
$website->getId()
188201
);
189202
}
@@ -194,7 +207,7 @@ private function migrateStoresAllowedCountriesToWebsite(SetupInterface $setup)
194207
$connection->delete(
195208
$setup->getTable('core_config_data'),
196209
[
197-
'path = ?' => CountryHandlerInterface::ALLOWED_COUNTRIES_PATH,
210+
'path = ?' => AllowedCountries::ALLOWED_COUNTRIES_PATH,
198211
'scope = ?' => ScopeInterface::SCOPE_STORES
199212
]
200213
);
@@ -207,7 +220,7 @@ private function migrateStoresAllowedCountriesToWebsite(SetupInterface $setup)
207220
'value' => implode(',', $countries)
208221
],
209222
[
210-
'path = ?' => CountryHandlerInterface::ALLOWED_COUNTRIES_PATH,
223+
'path = ?' => AllowedCountries::ALLOWED_COUNTRIES_PATH,
211224
'scope_id = ?' => $scopeId,
212225
'scope = ?' => ScopeInterface::SCOPE_WEBSITES
213226
]

0 commit comments

Comments
 (0)