6

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.

1
  • Can someone help me with this problem?
    – Despotars
    Commented Jun 4, 2019 at 8:27

2 Answers 2

2
+50

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);
        }
    }
}
29
  • The ID of my store view is 12, what option should I use? $storeCode = '12'; or $storeCode = 12';
    – Despotars
    Commented Jun 4, 2019 at 9:00
  • No use your store code.. Commented Jun 4, 2019 at 9:01
  • 1
    use with quotes.... Commented Jun 4, 2019 at 9:06
  • 1
    No just change the plugin file only..../app/code/MyPlugin/CustomerLogin/Plugin/LoginPostPlugin.php Commented Jun 4, 2019 at 9:11
  • 1
    Will update the answer shortly Commented Jun 4, 2019 at 9:34
3

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.

21
  • What number should I put here? $this->_storeManagerInterface->setCurrentStore(1);
    – Despotars
    Commented May 29, 2019 at 12:22
  • store=Wholesale storeId Commented May 29, 2019 at 12:26
  • for this: $result->setPath('?___store=Wholesale&___from_store=Wholesale'); Is it possible to do something so that this data does not appear in the URL?
    – Despotars
    Commented May 29, 2019 at 12:32
  • Hi @chirag-patel, I used the code that you showed me, that put the ID of the store view that I want to show, but it does not work. Any ideas?
    – Despotars
    Commented May 29, 2019 at 13:36
  • You can remove ?___store=Wholesale&___from_store=Wholesale this from system config Go to Store > config > general > web > URL option > Add Store Code to Urls set to NO Commented May 29, 2019 at 14:04

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.