/** * @return void */ public function execute() { $currentActiveStore = $this->storeManager->getStore(); $storeCode = $this->_request->getParam(StoreResolver::PARAM_NAME, $this->storeCookieManager->getStoreCodeFromCookie()); try { $store = $this->storeRepository->getActiveStoreByCode($storeCode); } catch (StoreIsInactiveException $e) { $error = __('Requested store is inactive'); } catch (NoSuchEntityException $e) { $error = __('Requested store is not found'); } if (isset($error)) { $this->messageManager->addError($error); $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl()); return; } $defaultStoreView = $this->storeManager->getDefaultStoreView(); if ($defaultStoreView->getId() == $store->getId()) { $this->storeCookieManager->deleteStoreCookie($store); } else { $this->httpContext->setValue(Store::ENTITY, $store->getCode(), $defaultStoreView->getCode()); $this->storeCookieManager->setStoreCookie($store); } if ($store->isUseStoreInUrl()) { // Change store code in redirect url if (strpos($this->_redirect->getRedirectUrl(), $currentActiveStore->getBaseUrl()) !== false) { $this->getResponse()->setRedirect(str_replace($currentActiveStore->getBaseUrl(), $store->getBaseUrl(), $this->_redirect->getRedirectUrl())); } else { $this->getResponse()->setRedirect($store->getBaseUrl()); } } else { $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl()); } }
/** * Delete cookie "store" if the store (a value in the cookie) does not exist or is inactive * * @param \Magento\Framework\App\FrontController $subject * @param \Magento\Framework\App\RequestInterface $request * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeDispatch(\Magento\Framework\App\FrontController $subject, \Magento\Framework\App\RequestInterface $request) { $storeCodeFromCookie = $this->storeCookieManager->getStoreCodeFromCookie(); if ($storeCodeFromCookie) { try { $this->storeRepository->getActiveStoreByCode($storeCodeFromCookie); } catch (StoreIsInactiveException $e) { $this->storeCookieManager->deleteStoreCookie($this->storeManager->getDefaultStoreView()); } catch (NoSuchEntityException $e) { $this->storeCookieManager->deleteStoreCookie($this->storeManager->getDefaultStoreView()); } catch (InvalidArgumentException $e) { $this->storeCookieManager->deleteStoreCookie($this->storeManager->getDefaultStoreView()); } } }