storeCookieManager = $storeCookieManager; $this->httpContext = $httpContext; $this->storeRepository = $storeRepository; $this->storeManager = $storeManager; $this->messageManager = $context->getMessageManager(); $this->storeSwitcher = $storeSwitcher ?: ObjectManager::getInstance()->get(StoreSwitcherInterface::class); } /** * Execute action * * @return void * @throws StoreSwitcher\CannotSwitchStoreException */ public function execute() { $targetStoreCode = $this->_request->getParam( \Magento\Store\Model\StoreManagerInterface::PARAM_NAME, $this->storeCookieManager->getStoreCodeFromCookie() ); $fromStoreCode = $this->_request->getParam('___from_store'); $requestedUrlToRedirect = $this->_redirect->getRedirectUrl(); $redirectUrl = $requestedUrlToRedirect; $error = null; try { $fromStore = $this->storeRepository->get($fromStoreCode); $targetStore = $this->storeRepository->getActiveStoreByCode($targetStoreCode); } catch (StoreIsInactiveException $e) { $error = __('Requested store is inactive'); } catch (NoSuchEntityException $e) { $error = __("The store that was requested wasn't found. Verify the store and try again."); } if ($error !== null) { $this->messageManager->addErrorMessage($error); } else { $redirectUrl = $this->storeSwitcher->switch($fromStore, $targetStore, $requestedUrlToRedirect); } $this->getResponse()->setRedirect($redirectUrl); } }