0

My site requires customers to login via third party to a multistore on magento 2 using the same url (no store code in url). I have 3 websites with corresponding stores, views and customer groups.

Ideally, I want a system where every customer logs into the same default store/website and gets automatically switched to a specific store based on their customer group once logged in.

At the moment I can only use the store cookie to switch between stores and that's about it. So far all i am unable to find solutions that don't include subdomains (i have to keep the url the same for each store).

I've found the below articles but neither seems to help

https://community.magento.com/t5/Magento-2-x-Admin-Configuration/Magento-2-Multi-website-single-domain-configuration/td-p/94275

Magento 2: Multistore with the same domain

Is this doable? Can someone point me in the right direction?

1
  • I should add that the complication in this is the default website. I can set any of the 3 websites as the default site and the customers can log in with no issues.
    – Roland142
    Aug 11, 2023 at 4:42

1 Answer 1

0

You can redirect store based on customer group after login.

Please try below code and manage as per your need.

Create events.xml file

Path: app/code/Mpr/StoreViewSwitcher/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_login">
        <observer name="customer_login_observer" instance="Mpr\StoreViewSwitcher\Observer\CustomerLogin" />
    </event>

</config>

Create CustomerLogin.php file

Path: app/code/Mpr/StoreViewSwitcher/Observer/CustomerLogin.php

<?php

namespace Mpr\StoreViewSwitcher\Observer;

use Magento\Store\Model\Store;
use Magento\Framework\Event\ObserverInterface;

class CustomerLogin implements ObserverInterface
{
    
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManagerInterface;

    protected $httpContext;

    protected $storeCookieManager;

    protected $storeRepository;
    
    public function __construct(
        \Magento\Framework\App\Http\Context  $httpContext,
        \Magento\Store\Api\StoreCookieManagerInterface  $storeCookieManager,
        \Magento\Store\Api\StoreRepositoryInterface  $storeRepository,
        \Magento\Store\Model\StoreManagerInterface $storeManagerInterface
    ) {
        $this->httpContext              = $httpContext;
        $this->storeCookieManager       = $storeCookieManager;
        $this->storeRepository          = $storeRepository;
        $this->_storeManagerInterface   = $storeManagerInterface;
    }


    /**
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer) {   

        $customer = $observer->getEvent()->getCustomer();
        
        //$mycode    = strtolower($customer->getMycode());   
        $mycode    = 'your customer group code';  // make change as per requirement
        
        switch ($mycode) {
            case "storeViewCode1":
                $store = $this->storeRepository->getActiveStoreByCode('storeViewCode1'); // storeViewCode1 your store view code
                $this->httpContext->setValue(Store::ENTITY, 'storeViewCode1', 'english'); // english is default store view code
                $this->storeCookieManager->setStoreCookie($store);        
                break;
            case "storeViewCode2":
                $store = $this->storeRepository->getActiveStoreByCode('storeViewCode2');
                $this->httpContext->setValue(Store::ENTITY, 'storeViewCode2', 'english');
                $this->storeCookieManager->setStoreCookie($store);                  
                break;
            default:
                $store = $this->storeRepository->getActiveStoreByCode('english');
                $this->httpContext->setValue(Store::ENTITY, 'english', 'english');
                $this->storeCookieManager->setStoreCookie($store);                
        }
        
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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