|
6 | 6 |
|
7 | 7 | namespace Magento\Directory\Helper; |
8 | 8 |
|
| 9 | +use Magento\Directory\Model\AllowedCountries; |
9 | 10 | use Magento\Directory\Model\Currency; |
10 | 11 | use Magento\Directory\Model\CurrencyFactory; |
11 | 12 | use Magento\Directory\Model\ResourceModel\Country\Collection; |
12 | 13 | use Magento\Directory\Model\ResourceModel\Region\CollectionFactory; |
13 | 14 | use Magento\Framework\App\Cache\Type\Config; |
| 15 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
14 | 16 | use Magento\Framework\App\Helper\Context; |
15 | 17 | use Magento\Framework\Json\Helper\Data as JsonData; |
16 | 18 | use Magento\Store\Model\ScopeInterface; |
|
21 | 23 | * |
22 | 24 | * @api |
23 | 25 | * @since 100.0.2 |
| 26 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
24 | 27 | */ |
25 | 28 | class Data extends \Magento\Framework\App\Helper\AbstractHelper |
26 | 29 | { |
@@ -156,6 +159,7 @@ public function getRegionCollection() |
156 | 159 | { |
157 | 160 | if (!$this->_regionCollection) { |
158 | 161 | $this->_regionCollection = $this->_regCollectionFactory->create(); |
| 162 | + // phpstan:ignore |
159 | 163 | $this->_regionCollection->addCountryFilter($this->getAddress()->getCountryId())->load(); |
160 | 164 | } |
161 | 165 | return $this->_regionCollection; |
@@ -185,7 +189,9 @@ public function getRegionJson() |
185 | 189 | { |
186 | 190 | \Magento\Framework\Profiler::start('TEST: ' . __METHOD__, ['group' => 'TEST', 'method' => __METHOD__]); |
187 | 191 | if (!$this->_regionJson) { |
188 | | - $cacheKey = 'DIRECTORY_REGIONS_JSON_STORE' . $this->_storeManager->getStore()->getId(); |
| 192 | + $scope = $this->getCurrentScope(); |
| 193 | + $scopeKey = $scope['value'] ? '_' . implode('_', $scope) : null; |
| 194 | + $cacheKey = 'DIRECTORY_REGIONS_JSON_STORE' . $scopeKey; |
189 | 195 | $json = $this->_configCacheType->load($cacheKey); |
190 | 196 | if (empty($json)) { |
191 | 197 | $regions = $this->getRegionData(); |
@@ -344,10 +350,13 @@ public function getDefaultCountry($store = null) |
344 | 350 | */ |
345 | 351 | public function getRegionData() |
346 | 352 | { |
347 | | - $countryIds = []; |
348 | | - foreach ($this->getCountryCollection() as $country) { |
349 | | - $countryIds[] = $country->getCountryId(); |
350 | | - } |
| 353 | + $scope = $this->getCurrentScope(); |
| 354 | + $allowedCountries = $this->scopeConfig->getValue( |
| 355 | + AllowedCountries::ALLOWED_COUNTRIES_PATH, |
| 356 | + $scope['type'], |
| 357 | + $scope['value'] |
| 358 | + ); |
| 359 | + $countryIds = explode(',', $allowedCountries); |
351 | 360 | $collection = $this->_regCollectionFactory->create(); |
352 | 361 | $collection->addCountryFilter($countryIds)->load(); |
353 | 362 | $regions = [ |
@@ -392,4 +401,31 @@ public function getWeightUnit() |
392 | 401 | { |
393 | 402 | return $this->scopeConfig->getValue(self::XML_PATH_WEIGHT_UNIT, ScopeInterface::SCOPE_STORE); |
394 | 403 | } |
| 404 | + |
| 405 | + /** |
| 406 | + * Get current scope from request |
| 407 | + * |
| 408 | + * @return array |
| 409 | + */ |
| 410 | + private function getCurrentScope(): array |
| 411 | + { |
| 412 | + $scope = [ |
| 413 | + 'type' => ScopeConfigInterface::SCOPE_TYPE_DEFAULT, |
| 414 | + 'value' => null, |
| 415 | + ]; |
| 416 | + $request = $this->_getRequest(); |
| 417 | + if ($request->getParam(ScopeInterface::SCOPE_WEBSITE)) { |
| 418 | + $scope = [ |
| 419 | + 'type' => ScopeInterface::SCOPE_WEBSITE, |
| 420 | + 'value' => $request->getParam(ScopeInterface::SCOPE_WEBSITE), |
| 421 | + ]; |
| 422 | + } elseif ($request->getParam(ScopeInterface::SCOPE_STORE)) { |
| 423 | + $scope = [ |
| 424 | + 'type' => ScopeInterface::SCOPE_STORE, |
| 425 | + 'value' => $request->getParam(ScopeInterface::SCOPE_STORE), |
| 426 | + ]; |
| 427 | + } |
| 428 | + |
| 429 | + return $scope; |
| 430 | + } |
395 | 431 | } |
0 commit comments