1313use Magento \Framework \Exception \NotFoundException ;
1414use Magento \Store \Api \Data \StoreInterface ;
1515use Magento \Store \Api \StoreCookieManagerInterface ;
16+ use Magento \Store \Model \ScopeInterface ;
17+ use Magento \Store \Model \StoreManager ;
1618use Magento \Store \Model \StoreManagerInterface ;
1719
1820/**
@@ -83,36 +85,37 @@ public function beforeDispatch(
8385 );
8486 if (is_array ($ storeCode )) {
8587 if (!isset ($ storeCode ['_data ' ]['code ' ])) {
86- $ this ->processInvalidStoreRequested ();
88+ $ this ->processInvalidStoreRequested ($ request );
8789 }
8890 $ storeCode = $ storeCode ['_data ' ]['code ' ];
8991 }
9092 if ($ storeCode === '' ) {
9193 //Empty code - is an invalid code and it was given explicitly
9294 //(the value would be null if the code wasn't found).
93- $ this ->processInvalidStoreRequested ();
95+ $ this ->processInvalidStoreRequested ($ request );
9496 }
9597 try {
9698 $ currentStore = $ this ->storeManager ->getStore ($ storeCode );
99+ $ this ->updateContext ($ request , $ currentStore );
97100 } catch (NoSuchEntityException $ exception ) {
98- $ this ->processInvalidStoreRequested ($ exception );
101+ $ this ->processInvalidStoreRequested ($ request , $ exception );
99102 }
100-
101- $ this ->updateContext ($ currentStore );
102103 }
103104
104105 /**
105106 * Take action in case of invalid store requested.
106107 *
108+ * @param RequestInterface $request
107109 * @param \Throwable|null $previousException
108110 * @return void
109111 * @throws NotFoundException
110112 */
111113 private function processInvalidStoreRequested (
114+ RequestInterface $ request ,
112115 \Throwable $ previousException = null
113116 ) {
114117 $ store = $ this ->storeManager ->getStore ();
115- $ this ->updateContext ($ store );
118+ $ this ->updateContext ($ request , $ store );
116119
117120 throw new NotFoundException (
118121 $ previousException
@@ -125,16 +128,28 @@ private function processInvalidStoreRequested(
125128 /**
126129 * Update context accordingly to the store found.
127130 *
131+ * @param RequestInterface $request
128132 * @param StoreInterface $store
129133 * @return void
130134 * @throws \Magento\Framework\Exception\LocalizedException
131135 */
132- private function updateContext (StoreInterface $ store )
136+ private function updateContext (RequestInterface $ request , StoreInterface $ store )
133137 {
138+ switch (true ) {
139+ case $ store ->isUseStoreInUrl ():
140+ $ defaultStoreCode = $ store ->getCode ();
141+ break ;
142+ case ScopeInterface::SCOPE_STORE == $ request ->getServerValue (StoreManager::PARAM_RUN_TYPE ):
143+ $ defaultStoreCode = $ request ->getServerValue (StoreManager::PARAM_RUN_CODE );
144+ break ;
145+ default :
146+ $ defaultStoreCode = $ this ->storeManager ->getDefaultStoreView ()->getCode ();
147+ break ;
148+ }
134149 $ this ->httpContext ->setValue (
135150 StoreManagerInterface::CONTEXT_STORE ,
136151 $ store ->getCode (),
137- $ store -> isUseStoreInUrl () ? $ store -> getCode () : $ this -> storeManager -> getDefaultStoreView ()-> getCode ()
152+ $ defaultStoreCode
138153 );
139154
140155 /** @var StoreInterface $defaultStore */
0 commit comments