Search code examples
magento2handlerstore

Change store view automatically for different customer groups in Magento 2


I'm currently using Magento 2.3.2 and I would like to show certain customers a specific store view based on their customer group. (For example a customer in the "General" group would see the default store view, while a customer in the "Platinum" group would see the "Platinum" store view with a slightly different logo and design).

Is there an extension out there that can do this? I can only find ones which restrict the products in the catalog?

Edit 20/02/2020 -

Thanks to Invigorate Systems for the solution. I have now implemented the code as below in the app > code folders:

registration.php file inside GroupSite/SiteSwitch/

<?php
    \Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'GroupSite_SiteSwitch',
    __DIR__
    );

module.xml file inside GroupSite/SiteSwitch/etc/

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
            <module name="GroupSite_SiteSwitch" setup_version="2.1.1"></module>
</config>

events.xml inside GroupSite/SiteSwitch/etc/frontend/

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd">
    <event name="layout_load_before">
        <observer name="add_layout_handles" instance="GroupSite\SiteSwitch\Observer\AddHandles" />
    </event>
</config>

AddHandles.php file inside GroupSite/SiteSwitch/Observer

<?php

namespace GroupSite\SiteSwitch\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Model\Session as CustomerSession;

class AddHandles implements ObserverInterface
{
    protected $customerSession;
    protected $_storeManager;
    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        CustomerSession $customerSession
    ) {
        $this->customerSession = $customerSession;
        $this->_storeManager = $storeManager;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $layout = $observer->getEvent()->getLayout();
         if ($this->customerSession->isLoggedIn()) 
             {
             $customerGroup = $this->customerSession->getCustomer()->getGroupId();
                if($customerGroup === '5'){
                    $this->_storeManager->setCurrentStore('13'); //Set your desired store ID that you wish to set.
                }
                else{
                    $this->_storeManager->setCurrentStore('1');         
                }
             }
    }
}

Solution

  • You can do this by using Observers, here's an example module for you. this module will change store ID after customer login to the system.

    1. Create registration.php file inside Vendor/Module/
    <?php
        \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Vendor_Module',
        __DIR__
        );
    
    1. Create module.xml file inside Vendor/Module/etc/
    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
                <module name="Vendor_Module" setup_version="2.1.1"></module>
    </config>
    
    1. Create events.xml inside Vendor/Module/etc/frontend/
    <?xml version="1.0" encoding="UTF-8"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd">
        <event name="layout_load_before">
            <observer name="add_layout_handles" instance="Vendor\Module\Observer\AddHandles" />
        </event>
    </config>
    
    1. Create Handler file for Observer AddHandles.php file inside Vendor/Module/Observer
    <?php
    
    namespace Vendor\Module\Observer;
    
    use Magento\Framework\Event\Observer;
    use Magento\Framework\Event\ObserverInterface;
    use Magento\Customer\Model\Session as CustomerSession;
    
    class AddHandles implements ObserverInterface
    {
        protected $customerSession;
        protected $_storeManager;
        public function __construct(
            \Magento\Store\Model\StoreManagerInterface $storeManager,
            CustomerSession $customerSession
        ) {
            $this->customerSession = $customerSession;
            $this->_storeManager = $storeManager;
        }
    
        public function execute(\Magento\Framework\Event\Observer $observer)
        {
            $layout = $observer->getEvent()->getLayout();
    
            if ($this->customerSession->isLoggedIn())
            {
                /*
                Here you fetch loggedIn Customer Group and add if condition such as
                if(customerGroup == 'ID/Name of group you desire'){
                    $this->_storeManager->setCurrentStore('2'); //Set your desired store ID that you wish to set.
                }
                */
                $this->_storeManager->setCurrentStore('2');
            }
        }
    }