Skip to content

Commit 7366763

Browse files
Merge pull request #2106 from magento-qwerty/2.2-bugfixes-140218
Fixed issues: - MAGETWO-84074: Automate "Update Configurable Product (based on Swatch Attribute) from Shopping Cart" FT - MAGETWO-71790: Accessing url with ___store= creates exception
2 parents d9fc848 + 685e822 commit 7366763

File tree

22 files changed

+891
-75
lines changed

22 files changed

+891
-75
lines changed

app/code/Magento/Store/App/Action/Plugin/Context.php

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
namespace Magento\Store\App\Action\Plugin;
88

99
use Magento\Framework\App\Http\Context as HttpContext;
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Framework\Exception\NotFoundException;
1012
use Magento\Framework\Phrase;
13+
use Magento\Store\Api\Data\StoreInterface;
1114
use Magento\Store\Api\StoreCookieManagerInterface;
1215
use Magento\Store\Api\StoreResolverInterface;
1316
use Magento\Store\Model\StoreManagerInterface;
@@ -65,35 +68,94 @@ public function __construct(
6568
* @return void
6669
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
6770
*/
68-
public function beforeDispatch(AbstractAction $subject, RequestInterface $request)
69-
{
70-
/** @var \Magento\Store\Model\Store $defaultStore */
71-
$defaultStore = $this->storeManager->getWebsite()->getDefaultStore();
71+
public function beforeDispatch(
72+
AbstractAction $subject,
73+
RequestInterface $request
74+
) {
75+
if ($this->isAlreadySet()) {
76+
//If required store related value were already set for
77+
//HTTP processors then just continuing as we were.
78+
return;
79+
}
7280

81+
/** @var string|array|null $storeCode */
7382
$storeCode = $request->getParam(
7483
StoreResolverInterface::PARAM_NAME,
7584
$this->storeCookieManager->getStoreCodeFromCookie()
7685
);
77-
7886
if (is_array($storeCode)) {
7987
if (!isset($storeCode['_data']['code'])) {
80-
throw new \InvalidArgumentException(new Phrase('Invalid store parameter.'));
88+
$this->processInvalidStoreRequested();
8189
}
8290
$storeCode = $storeCode['_data']['code'];
8391
}
84-
/** @var \Magento\Store\Model\Store $currentStore */
85-
$currentStore = $storeCode ? $this->storeManager->getStore($storeCode) : $defaultStore;
92+
if ($storeCode === '') {
93+
//Empty code - is an invalid code and it was given explicitly
94+
//(the value would be null if the code wasn't found).
95+
$this->processInvalidStoreRequested();
96+
}
97+
try {
98+
$currentStore = $this->storeManager->getStore($storeCode);
99+
} catch (NoSuchEntityException $exception) {
100+
$this->processInvalidStoreRequested($exception);
101+
}
102+
103+
$this->updateContext($currentStore);
104+
}
105+
106+
/**
107+
* Take action in case of invalid store requested.
108+
*
109+
* @param \Throwable|null $previousException
110+
*
111+
* @throws NotFoundException
112+
*/
113+
private function processInvalidStoreRequested(
114+
\Throwable $previousException = null
115+
) {
116+
$store = $this->storeManager->getStore();
117+
$this->updateContext($store);
86118

119+
throw new NotFoundException(
120+
$previousException
121+
? __($previousException->getMessage())
122+
: __('Invalid store requested.'),
123+
$previousException
124+
);
125+
}
126+
127+
/**
128+
* Update context accordingly to the store found.
129+
*
130+
* @param StoreInterface $store
131+
*/
132+
private function updateContext(StoreInterface $store)
133+
{
87134
$this->httpContext->setValue(
88135
StoreManagerInterface::CONTEXT_STORE,
89-
$currentStore->getCode(),
136+
$store->getCode(),
90137
$this->storeManager->getDefaultStoreView()->getCode()
91138
);
92139

140+
/** @var StoreInterface $defaultStore */
141+
$defaultStore = $this->storeManager->getWebsite()->getDefaultStore();
93142
$this->httpContext->setValue(
94143
HttpContext::CONTEXT_CURRENCY,
95-
$this->session->getCurrencyCode() ?: $currentStore->getDefaultCurrencyCode(),
144+
$this->session->getCurrencyCode()
145+
?: $store->getDefaultCurrencyCode(),
96146
$defaultStore->getDefaultCurrencyCode()
97147
);
98148
}
149+
150+
/**
151+
* Check if there a need to find the current store.
152+
*
153+
* @return bool
154+
*/
155+
private function isAlreadySet(): bool
156+
{
157+
$storeKey = StoreManagerInterface::CONTEXT_STORE;
158+
159+
return $this->httpContext->getValue($storeKey) !== null;
160+
}
99161
}

app/code/Magento/Store/Model/StoreManagerInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\Store\Model;
88

9+
use Magento\Framework\Exception\NoSuchEntityException;
10+
911
/**
1012
* Store manager interface
1113
*
@@ -46,6 +48,7 @@ public function isSingleStoreMode();
4648
*
4749
* @param null|string|bool|int|\Magento\Store\Api\Data\StoreInterface $storeId
4850
* @return \Magento\Store\Api\Data\StoreInterface
51+
* @throws NoSuchEntityException If given store doesn't exist.
4952
*/
5053
public function getStore($storeId = null);
5154

0 commit comments

Comments
 (0)