Skip to content

Commit 4ed6a2b

Browse files
author
Sergii Kovalenko
committed
MAGETWO-56936: [Backport][Github] Allowed countries for customer address in admin using storeview configuration #2946
1 parent 37d35ca commit 4ed6a2b

File tree

6 files changed

+65
-35
lines changed

6 files changed

+65
-35
lines changed

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/Country.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ protected function _createCountriesCollection()
6565

6666
/**
6767
* Retrieve Store Resolver
68+
*
6869
* @deprecated
6970
* @return StoreResolverInterface
7071
*/

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
132132
}
133133

134134
/**
135+
* Retrieve Store Manager
136+
*
135137
* @deprecated
136138
* @return StoreManagerInterface
137139
*/
@@ -146,6 +148,7 @@ private function getStoreManager()
146148

147149
/**
148150
* Retrieve Allowed Countries Reader
151+
*
149152
* @deprecated
150153
* @return AllowedCountries
151154
*/
@@ -159,6 +162,8 @@ private function getAllowedCountriesReader()
159162
}
160163

161164
/**
165+
* Merge allowed countries from different scopes
166+
*
162167
* @param array $countries
163168
* @param array $newCountries
164169
* @param string $identifier
@@ -177,6 +182,8 @@ private function mergeAllowedCountries(array $countries, array $newCountries, $i
177182
}
178183

179184
/**
185+
* Migrate and merge allowed countries
186+
*
180187
* @param SetupInterface $setup
181188
* @return void
182189
*/

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 AllowedCountries.
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: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
5959
/**
6060
* @var AllowedCountries
6161
*/
62-
private $allowedCountries;
62+
private $allowedCountriesReader;
6363

6464
/**
6565
* @var string[]
@@ -128,27 +128,29 @@ protected function _construct()
128128

129129
/**
130130
* Return Allowed Countries reader
131+
*
131132
* @deprecated
132133
* @return \Magento\Directory\Model\AllowedCountries
133134
*/
134135
private function getAllowedCountriesReader()
135136
{
136-
if (!$this->allowedCountries) {
137-
$this->allowedCountries = ObjectManager::getInstance()->get(AllowedCountries::class);
137+
if (!$this->allowedCountriesReader) {
138+
$this->allowedCountriesReader = ObjectManager::getInstance()->get(AllowedCountries::class);
138139
}
139140

140-
return $this->allowedCountries;
141+
return $this->allowedCountriesReader;
141142
}
142143

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

153155
if (!empty($allowedCountries)) {
154156
$this->addFieldToFilter("country_id", ['in' => $allowedCountries]);
@@ -157,17 +159,6 @@ private function loadByScope($filter, $scope = ScopeInterface::SCOPE_STORE)
157159
return $this;
158160
}
159161

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-
171162
/**
172163
* Loads Item By Id
173164
*

0 commit comments

Comments
 (0)