Question

I have created two store views, one for B2C and one for B2B. I need that when a B2B customer login will be redirected automatically to the view created for their user group. But for B2C customers can use the normal store view. I'm creating a plugin to do this. I created these files in these paths:

/app/code/MyPlugin/CustomerLogin/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="\Magento\Customer\Controller\Account\LoginPost">
        <plugin name="myPluginCustomerloginLoginpostplugin" type="\MyPlugin\CustomerLogin\Plugin\LoginPostPlugin" sortOrder="1" />
    </type>
</config>

/app/code/MyPlugin/CustomerLogin/Plugin/LoginPostPlugin.php

<?php

namespace MyPlugin\CustomerLogin\Plugin;

class LoginPostPlugin
{
    public function afterExecute(
        \Magento\Customer\Controller\Account\LoginPost $subject,
        $result)
    {
        //-- check group is retail customer or not
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerSession = $objectManager->create('Magento\Customer\Model\Session');
        if ($customerSession->isLoggedIn()){
            $groupId = $customerSession->getCustomerGroupId();
            if ($groupId == 2){
                $result->setPath('?___store=Wholesale&___from_store=Wholesale');
            }
        }
        return $result;
    }
}

And the view changes to the selected view (wholesale), but when I open a product or a category, the view changes to the predefined view, the change does not become effective. Any other idea of being able to make the fixed exchange?

This is the line that I do not know what to put in order that the scope of view remains:

$result->setPath('?___store=Wholesale&___from_store=Wholesale');

Any ideas to solve this problem? I need to make a change of store view and keep the change.

Was it helpful?

Solution

Try to set store in cookie with specific store code by using below code

    protected $_storeRepository;

    protected $_storeCookieManager;

    protected $_storeManagerInterface;

    public function __construct(
        \Magento\Store\Api\StoreRepositoryInterface $storeRepository,
        \Magento\Store\Api\StoreCookieManagerInterface $StoreCookieManagerInterface,
        \Magento\Store\Model\StoreManagerInterface $storeManagerInterface
    )
    {
        $this->_storeRepository = $storeRepository;
        $this->_storeCookieManager = $StoreCookieManagerInterface;
        $this->_storeManagerInterface = $storeManagerInterface;
    }

    public function afterExecute(
        \Magento\Customer\Controller\Account\LoginPost $subject,
        $result)
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerSession = $objectManager->create('Magento\Customer\Model\Session');
        if ($customerSession->isLoggedIn()){
            $groupId = $customerSession->getCustomerGroupId();
            if ($groupId == 2){
                $storeCode = 'your_store_code1';
            } else {
                $storeCode = 'your_store_code2';
            }
            $store = $this->_storeRepository->getActiveStoreByCode($storeCode);
            $this->_storeCookieManager->setStoreCookie($store);

        }
        return $result;
    }

To change store view to default store view use controller_action_predispatch in /app/code/MyPlugin/CustomerLogin/etc/frontend/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="controller_action_predispatch">
        <observer name="store_switcher" instance="MyPlugin\CustomerLogin\Observer\SwitchStore" />
    </event>
</config>

Your /app/code/MyPlugin\CustomerLogin\Observer\SwitchStore.php will check the user loggedin or not to change store view.

namespace MyPlugin\CustomerLogin\Observer;

class SwitchStore implements \Magento\Framework\Event\ObserverInterface
{

    protected $_storeRepository;

    protected $_storeCookieManager;

    protected $_customerSession;

    public function __construct(
        \Magento\Store\Api\StoreRepositoryInterface $storeRepository,
        \Magento\Store\Api\StoreCookieManagerInterface $StoreCookieManagerInterface,
        \Magento\Customer\Model\SessionFactory $customerSession
    )
    {
        $this->_storeRepository = $storeRepository;
        $this->_storeCookieManager = $StoreCookieManagerInterface;
        $this->_customerSession = $customerSession->create();
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        if (!$this->_customerSession->isLoggedIn()) {
            $storeCode = 'default_store_code';
            $store = $this->_storeRepository->getActiveStoreByCode($storeCode);
            $this->_storeCookieManager->setStoreCookie($store);
        }
    }
}

OTHER TIPS

Yes, it will redirect to specific store view but when you click any page it will redirect to a default store page. because you set only redirect path not set store, so once go to another page it will redirect to default store.

You need to also set store as well. once the store is set it will affect the whole page.

You can set the store using below way.

Inject StoreManagerInterface in cunstruct.

<?php
namespace MyPlugin\CustomerLogin\Plugin;

class LoginPostPlugin
{

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManagerInterface;

    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManagerInterface
    ) {
        $this->_storeManagerInterface = $storeManagerInterface;
    }


    public function afterExecute(
        \Magento\Customer\Controller\Account\LoginPost $subject,
        $result)
    {
        //-- check group is retail customer or not
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerSession = $objectManager->create('Magento\Customer\Model\Session');
        if ($customerSession->isLoggedIn()){
            $groupId = $customerSession->getCustomerGroupId();
            if ($groupId == 2){
                $this->_storeManagerInterface->setCurrentStore(1);
                $result->setPath('?___store=Wholesale&___from_store=Wholesale');
            }
        }
        return $result;
    }
}

I hope it helps! Thanks.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top