I have created a custom module that observes the Login button so that when it is clicked I can switch the store view to a different store view. So far, this appears to be mostly working. However, upon login I get stuck in a redirect loop between the customer\account\loginPost URL and the store switching URL stores/store/switch/?___from_store=gb&___store=es. My code is as follows:

public function afterExecute(\Magento\Customer\Controller\Account\LoginPost $subject, $result){
    $urlOnTargetStore = $this->_store->getCurrentUrl(false);
    $baseUrl = $this->_storeManagerInterface->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK);
    $switchStore = "stores/store/switch/?___from_store=gb&___store=es&uenc=" . $this->_urlHelper->getEncodedUrl($urlOnTargetStore);
    $result->setUrl($baseUrl . $switchStore);

    $this->_logger->info("Encoded URL: " . $baseUrl . $switchStore);
    return $result;

This automatically appends the Base64 encoded parameter to the end of the URL just like the regular store switcher contained within the footer. My goal is to switch to the spanish store view using the URL without it getting stuck in a redirect loop. Any help would be greatly appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.