Skip to content

Commit 8eb3cb4

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

File tree

7 files changed

+88
-49
lines changed

7 files changed

+88
-49
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
6161
/**
6262
* @var CountryWithWebsites
6363
*/
64-
private $countryByWebsiteSource;
64+
private $countryWithWebsiteSource;
6565

6666
/**
6767
* @var \Magento\Customer\Model\Config\Share
@@ -321,7 +321,7 @@ protected function getAttributesMeta(Type $entityType)
321321

322322
if ($attribute->usesSource()) {
323323
if ($code == AddressInterface::COUNTRY_ID) {
324-
$meta[$code]['arguments']['data']['config']['options'] = $this->getCountryByWebsiteSource()
324+
$meta[$code]['arguments']['data']['config']['options'] = $this->getcountryWithWebsiteSource()
325325
->getAllOptions();
326326
} else {
327327
$meta[$code]['arguments']['data']['config']['options'] = $attribute->getSource()->getAllOptions();
@@ -342,19 +342,23 @@ protected function getAttributesMeta(Type $entityType)
342342
}
343343

344344
/**
345+
* Retrieve Country With Websites Source
346+
*
345347
* @deprecated
346348
* @return CountryWithWebsites
347349
*/
348-
private function getCountryByWebsiteSource()
350+
private function getCountryWithWebsiteSource()
349351
{
350-
if (!$this->countryByWebsiteSource) {
351-
$this->countryByWebsiteSource = ObjectManager::getInstance()->get(CountryWithWebsites::class);
352+
if (!$this->countryWithWebsiteSource) {
353+
$this->countryWithWebsiteSource = ObjectManager::getInstance()->get(CountryWithWebsites::class);
352354
}
353355

354-
return $this->countryByWebsiteSource;
356+
return $this->countryWithWebsiteSource;
355357
}
356358

357359
/**
360+
* Retrieve Customer Config Share
361+
*
358362
* @deprecated
359363
* @return \Magento\Customer\Model\Config\Share
360364
*/
@@ -368,6 +372,8 @@ private function getShareConfig()
368372
}
369373

370374
/**
375+
* Add global scope parameter and filter options to website meta
376+
*
371377
* @param array $meta
372378
* @return void
373379
*/

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,24 @@ public function __construct(
4040

4141
/**
4242
* Retrieve all allowed countries or specific by scope depends on customer share setting
43+
*
4344
* @param \Magento\Directory\Model\AllowedCountries $subject
4445
* @param string | null $filter
4546
* @param string $scope
4647
*/
4748
public function beforeGetAllowedCountries(
4849
\Magento\Directory\Model\AllowedCountries $subject,
49-
$filter = null,
50+
$scopeCode = null,
5051
$scope = ScopeInterface::SCOPE_WEBSITE
5152
) {
5253
if ($this->shareConfig->isGlobalScope()) {
5354
//Check if we have shared accounts - than merge all website allowed countries
54-
$filter = array_map(function (WebsiteInterface $website) {
55+
$scopeCode = array_map(function (WebsiteInterface $website) {
5556
return $website->getId();
5657
}, $this->storeManager->getWebsites());
5758
$scope = ScopeInterface::SCOPE_WEBSITES;
5859
}
5960

60-
return [$filter, $scope];
61+
return [$scopeCode, $scope];
6162
}
6263
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ class CountryWithWebsites extends \Magento\Eav\Model\Entity\Attribute\Source\Tab
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 AllowedCountries $allowedCountries
49+
* @param AllowedCountries $allowedCountriesReader
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\AllowedCountries $allowedCountries,
56+
\Magento\Directory\Model\AllowedCountries $allowedCountriesReader,
5757
\Magento\Store\Model\StoreManagerInterface $storeManager,
5858
\Magento\Customer\Model\Config\Share $shareConfig
5959
) {
6060
$this->countriesFactory = $countriesFactory;
61-
$this->allowedCountriesReader = $allowedCountries;
61+
$this->allowedCountriesReader = $allowedCountriesReader;
6262
$this->storeManager = $storeManager;
6363
$this->shareConfig = $shareConfig;
6464
parent::__construct($attrOptionCollectionFactory, $attrOptionFactory);
@@ -106,6 +106,7 @@ public function getAllOptions()
106106

107107
/**
108108
* Create Countries Collection with all countries
109+
*
109110
* @return \Magento\Directory\Model\ResourceModel\Country\Collection
110111
*/
111112
private function createCountriesCollection()

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class UpgradeData implements UpgradeDataInterface
4646
*/
4747
protected $eavConfig;
4848

49+
/**
50+
* @var StoreManagerInterface
51+
*/
52+
private $storeManager;
53+
4954
/**
5055
* @param CustomerSetupFactory $customerSetupFactory
5156
* @param IndexerRegistry $indexerRegistry
@@ -135,16 +140,23 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
135140
}
136141

137142
/**
143+
* Retrieve Store Manager
144+
*
138145
* @deprecated
139146
* @return StoreManagerInterface
140147
*/
141148
private function getStoreManager()
142149
{
143-
return \Magento\Framework\App\ObjectManager::getInstance()->get(StoreManagerInterface::class);
150+
if (!$this->storeManager) {
151+
$this->storeManager = ObjectManager::getInstance()->get(StoreManagerInterface::class);
152+
}
153+
154+
return $this->storeManager;
144155
}
145156

146157
/**
147158
* Retrieve Allowed Countries Reader
159+
*
148160
* @deprecated
149161
* @return AllowedCountries
150162
*/
@@ -158,6 +170,8 @@ private function getAllowedCountriesReader()
158170
}
159171

160172
/**
173+
* Merge allowed countries between different scopes
174+
*
161175
* @param array $countries
162176
* @param array $newCountries
163177
* @param string $identifier
@@ -176,6 +190,8 @@ private function mergeAllowedCountries(array $countries, array $newCountries, $i
176190
}
177191

178192
/**
193+
* Merge allowed countries from stores to websites
194+
*
179195
* @param SetupInterface $setup
180196
* @return void
181197
*/

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

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Magento\Store\Model\StoreManagerInterface;
1313

1414
/**
15-
* Class CountryHandler.
15+
* Class CountryHandler
1616
*/
1717
class AllowedCountries
1818
{
@@ -43,38 +43,65 @@ public function __construct(
4343

4444
/**
4545
* Retrieve all allowed countries for scope or scopes
46-
* @param string | null $filter
46+
*
47+
* @param string | null $scopeCode
4748
* @param string $scope
4849
* @return array
4950
*/
5051
public function getAllowedCountries(
51-
$filter = null,
52+
$scopeCode = null,
5253
$scope = ScopeInterface::SCOPE_WEBSITE
5354
) {
54-
if (empty($filter)) {
55-
$filter = $this->storeManager->getWebsite()->getId();
55+
if (empty($scopeCode)) {
56+
$scopeCode = $this->getDefaultScopeCode($scope);
5657
}
5758

5859
switch ($scope) {
5960
case ScopeInterface::SCOPE_WEBSITES:
6061
case ScopeInterface::SCOPE_STORES:
6162
$allowedCountries = [];
62-
foreach ($filter as $singleFilter) {
63+
foreach ($scopeCode as $singleFilter) {
6364
$allowedCountries = array_merge(
6465
$allowedCountries,
6566
$this->getCountriesFromConfig($this->getSingleScope($scope), $singleFilter)
6667
);
6768
}
6869
break;
6970
default:
70-
$allowedCountries = $this->getCountriesFromConfig($scope, $filter);
71+
$allowedCountries = $this->getCountriesFromConfig($scope, $scopeCode);
7172
}
7273

7374
return $this->getUniqueCountries($allowedCountries);
7475
}
7576

7677
/**
77-
* Return Unique Countries by merging them by keys.
78+
* Resolve scope code by scope
79+
*
80+
* @throws \InvalidArgumentException
81+
* @param $scope
82+
* @return array|int
83+
*/
84+
private function getDefaultScopeCode($scope)
85+
{
86+
switch ($scope) {
87+
case ScopeInterface::SCOPE_WEBSITE:
88+
return $this->storeManager->getWebsite()->getId();
89+
case ScopeInterface::SCOPE_STORE:
90+
return $this->storeManager->getStore()->getId();
91+
case ScopeInterface::SCOPE_GROUP:
92+
return $this->storeManager->getGroup()->getId();
93+
case ScopeInterface::SCOPE_WEBSITES:
94+
return [$this->storeManager->getWebsite()->getId()];
95+
case ScopeInterface::SCOPE_STORES:
96+
return [$this->storeManager->getStore()->getId()];
97+
default:
98+
throw new \InvalidArgumentException("Invalid scope is specified");
99+
}
100+
}
101+
102+
/**
103+
* Return Unique Countries by merging them by keys
104+
*
78105
* @param array $allowedCountries
79106
* @return array
80107
*/
@@ -85,24 +112,26 @@ private function getUniqueCountries(array $allowedCountries)
85112

86113
/**
87114
* Takes countries from Countries Config data
115+
*
88116
* @param string $scope
89-
* @param int $filter
117+
* @param int $scopeCode
90118
* @return array
91119
*/
92-
private function getCountriesFromConfig($scope, $filter)
120+
private function getCountriesFromConfig($scope, $scopeCode)
93121
{
94122
return explode(
95123
',',
96124
(string) $this->scopeConfig->getValue(
97125
self::ALLOWED_COUNTRIES_PATH,
98126
$scope,
99-
$filter
127+
$scopeCode
100128
)
101129
);
102130
}
103131

104132
/**
105133
* Return Single Scope
134+
*
106135
* @param string $scope
107136
* @return string
108137
*/

app/code/Magento/Directory/Model/ResourceModel/Country/Collection.php

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Magento\Directory\Model\ResourceModel\Country;
1313
use Magento\Directory\Model\AllowedCountries;
1414
use Magento\Framework\App\ObjectManager;
15-
use Magento\Store\Model\ScopeInterface;
1615

1716
/**
1817
* Class Collection
@@ -59,7 +58,7 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
5958
/**
6059
* @var AllowedCountries
6160
*/
62-
private $allowedCountries;
61+
private $allowedCountriesReader;
6362

6463
/**
6564
* @var string[]
@@ -128,27 +127,28 @@ protected function _construct()
128127

129128
/**
130129
* Return Allowed Countries reader
130+
*
131131
* @deprecated
132132
* @return \Magento\Directory\Model\AllowedCountries
133133
*/
134134
private function getAllowedCountriesReader()
135135
{
136-
if (!$this->allowedCountries) {
137-
$this->allowedCountries = ObjectManager::getInstance()->get(AllowedCountries::class);
136+
if (!$this->allowedCountriesReader) {
137+
$this->allowedCountriesReader = ObjectManager::getInstance()->get(AllowedCountries::class);
138138
}
139139

140-
return $this->allowedCountries;
140+
return $this->allowedCountriesReader;
141141
}
142142

143143
/**
144-
* Apply allowed countries by specific scope: store, website, etc.
145-
* @param string $filter
146-
* @param string $scope
147-
* @return self
144+
* Load allowed countries for current store
145+
*
146+
* @param null|int|string|\Magento\Store\Model\Store $store
147+
* @return \Magento\Directory\Model\ResourceModel\Country\Collection
148148
*/
149-
private function loadByScope($filter, $scope = ScopeInterface::SCOPE_STORE)
149+
public function loadByStore($store = null)
150150
{
151-
$allowedCountries = $this->getAllowedCountriesReader()->getAllowedCountries($filter, $scope);
151+
$allowedCountries = $this->getAllowedCountriesReader()->getAllowedCountries($store);
152152

153153
if (!empty($allowedCountries)) {
154154
$this->addFieldToFilter("country_id", ['in' => $allowedCountries]);
@@ -157,17 +157,6 @@ private function loadByScope($filter, $scope = ScopeInterface::SCOPE_STORE)
157157
return $this;
158158
}
159159

160-
/**
161-
* Load allowed countries for current store
162-
*
163-
* @param null|int|string|\Magento\Store\Model\Store $store
164-
* @return \Magento\Directory\Model\ResourceModel\Country\Collection
165-
*/
166-
public function loadByStore($store = null)
167-
{
168-
return $this->loadByScope($store, ScopeInterface::SCOPE_STORE);
169-
}
170-
171160
/**
172161
* Loads Item By Id
173162
*

app/code/Magento/Sales/Block/Adminhtml/Order/Create/Form/Address.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
namespace Magento\Sales\Block\Adminhtml\Order\Create\Form;
77

88
use Magento\Backend\Model\Session\Quote;
9-
use Magento\Directory\Model\AllowedCountries;
10-
use Magento\Directory\Model\ResourceModel\Country\Collection;
119
use Magento\Framework\App\ObjectManager;
1210
use Magento\Framework\Data\Form\Element\AbstractElement;
1311
use Magento\Framework\Pricing\PriceCurrencyInterface;
14-
use Magento\Store\Model\ScopeInterface;
1512

1613
/**
1714
* Order create address form

0 commit comments

Comments
 (0)