4

I have a website switcher button on my Magento 2.3 site (not a store switcher).

When a customer logs in on the website - abc.magento.com, I programmatically switch the website to pqr.magento.com, it redirects to the new website but logs out my customer.

I tried setting "YES" for following Configuration Setting

General -> Web -> Session Validation Settings -> Used SID on storefront

But it didn't work.

Is there any way to prevent logging out and maintain the customer session.

3
  • First check this Stores --> Configuration --> Customers --> Customer Configuration: and set to Global
    – BartZalas
    Dec 10, 2019 at 18:47
  • It has already been set to Global, but still customer logs out
    – Nausif
    Dec 11, 2019 at 9:14
  • same issue for me,I have set to Global still its asking to login again. Anyone have any idea regarding it. Feb 26, 2021 at 10:04

1 Answer 1

0

I faced this issue recently, I found a solution based on the native : \Magento\Store\Model\StoreSwitcher and I have build the following controller, it create an url like : https://mysite1.local/store/change/?code=mysite2 and it keep the connexion if the customer was logged in the first site and if the customer accounts are shared between website.

<?php
namespace MyProject\Store\Controller\Change;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Url\EncoderInterface;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\ViewModel\SwitcherUrlProvider;

class Index extends Action
{
    protected $storeManager;
    protected $switcherUrlProvider;
    private $encoder;

    public function __construct(
        Context $context,
        StoreManagerInterface $storeManager,
        SwitcherUrlProvider $switcherUrlProvider,
        EncoderInterface $encoder
    ) {
        parent::__construct($context);
        $this->storeManager = $storeManager;
        $this->switcherUrlProvider = $switcherUrlProvider;
        $this->encoder = $encoder;
    }

    public function execute()
    {
        $code = $this->getRequest()->getParam('code');
        $store = $this->storeManager->getStore($code);
        $url = $this->_url->getUrl(
           'stores/store/redirect',
           [
                '___store' => $store->getCode(),
                '___from_store' => $this->storeManager->getStore()->getCode(),
                ActionInterface::PARAM_NAME_URL_ENCODED => $this->encoder->encode(
                    $store->getUrl('customer/account/login')
                ),
            ]
        );
        return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setUrl($url);
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.