Question

I am currently using an instance of Magento 2.3.2 I have now implemented the following code 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())
        {
            if(customerGroup == 'ID/5'){
                $this->_storeManager->setCurrentStore('trade'); 
            }
            $this->_storeManager->setCurrentStore('default');
        }
    }
}

Unfortunately I am now getting the error shown below:

1 exception(s):
Exception #0 (Exception): Notice: Use of undefined constant customerGroup - assumed 'customerGroup' in /var/www/vhosts/my.website.com/httpdocs/app/code/GroupSite/SiteSwitch/Observer/AddHandles.php on line 27

Exception #0 (Exception): Notice: Use of undefined constant customerGroup - assumed 'customerGroup' in /var/www/vhosts/my.website.com/httpdocs/app/code/GroupSite/SiteSwitch/Observer/AddHandles.php on line 27
<pre>#1 GroupSite\SiteSwitch\Observer\AddHandles->execute(&Magento\Framework\Event\Observer#00000000283595fd000000005f2d50d6#) called at [vendor/magento/framework/Event/Invoker/InvokerDefault.php:88]
#2 Magento\Framework\Event\Invoker\InvokerDefault->_callObserverMethod(&GroupSite\SiteSwitch\Observer\AddHandles#0000000028359206000000005f2d50d6#, &Magento\Framework\Event\Observer#00000000283595fd000000005f2d50d6#) called at [vendor/magento/framework/Event/Invoker/InvokerDefault.php:74]
#3 Magento\Framework\Event\Invoker\InvokerDefault->dispatch(array('instance' => 'GroupSite\SiteSw...', 'name' => 'add_layout_handl...'), &Magento\Framework\Event\Observer#00000000283595fd000000005f2d50d6#) called at [vendor/magento/framework/Event/Manager.php:66]
#4 Magento\Framework\Event\Manager->dispatch('layout_load_befo...', array('full_action_name' => 'catalog_category...', 'layout' => &Magento\Framework\View\Layout\Interceptor#000000002835a3c3000000005f2d50d6#)) called at [generated/code/Magento/Framework/Event/Manager/Proxy.php:95]
#5 Magento\Framework\Event\Manager\Proxy->dispatch('layout_load_befo...', array('full_action_name' => 'catalog_category...', 'layout' => &Magento\Framework\View\Layout\Interceptor#000000002835a3c3000000005f2d50d6#)) called at [vendor/magento/framework/View/Layout/Builder.php:81]
#6 Magento\Framework\View\Layout\Builder->loadLayoutUpdates() called at [vendor/magento/framework/View/Layout/Builder.php:63]
#7 Magento\Framework\View\Layout\Builder->build() called at [vendor/magento/framework/View/Page/Config.php:224]
#8 Magento\Framework\View\Page\Config->build() called at [vendor/magento/framework/View/Page/Config.php:587]
#9 Magento\Framework\View\Page\Config->getElementAttribute('body', 'class') called at [vendor/magento/framework/View/Page/Config.php:545]
#10 Magento\Framework\View\Page\Config->addBodyClass('page-products') called at [vendor/magento/module-catalog/Controller/Category/View.php:247]
#11 Magento\Catalog\Controller\Category\View->execute() called at [generated/code/Magento/Catalog/Controller/Category/View/Interceptor.php:24]
#12 Magento\Catalog\Controller\Category\View\Interceptor->execute() called at [vendor/magento/framework/App/Action/Action.php:108]
#13 Magento\Framework\App\Action\Action->dispatch(&Magento\Framework\App\Request\Http#000000002835a139000000005f2d50d6#) called at [vendor/magento/framework/Interception/Interceptor.php:58]
#14 Magento\Catalog\Controller\Category\View\Interceptor->___callParent('dispatch', array(&Magento\Framework\App\Request\Http#000000002835a139000000005f2d50d6#)) called at [vendor/magento/framework/Interception/Interceptor.php:138]
#15 Magento\Catalog\Controller\Category\View\Interceptor->Magento\Framework\Interception\{closure}(&Magento\Framework\App\Request\Http#000000002835a139000000005f2d50d6#) called at [vendor/magento/framework/Interception/Interceptor.php:153]
#16 Magento\Catalog\Controller\Category\View\Interceptor->___callPlugins('dispatch', array(&Magento\Framework\App\Request\Http#000000002835a139000000005f2d50d6#), array(array('designLoader', 'customerNotifica...', 'catalog_app_acti...', 'tax-app-action-d...', 'weee-app-action-...', 'storeCheck', 'contextPlugin', 'customer-app-act...'))) called at [generated/code/Magento/Catalog/Controller/Category/View/Interceptor.php:39]
#17 Magento\Catalog\Controller\Category\View\Interceptor->dispatch(&Magento\Framework\App\Request\Http#000000002835a139000000005f2d50d6#) called at [vendor/magento/framework/App/FrontController.php:159]
#18 Magento\Framework\App\FrontController->processRequest(&Magento\Framework\App\Request\Http#000000002835a139000000005f2d50d6#, &Magento\Catalog\Controller\Category\View\Interceptor#000000002835a8f9000000005f2d50d6#) called at [vendor/magento/framework/App/FrontController.php:99]
#19 Magento\Framework\App\FrontController->dispatch(&Magento\Framework\App\Request\Http#000000002835a139000000005f2d50d6#) called at [vendor/magento/framework/Interception/Interceptor.php:58]
#20 Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', array(&Magento\Framework\App\Request\Http#000000002835a139000000005f2d50d6#)) called at [vendor/magento/framework/Interception/Interceptor.php:138]
#21 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(&Magento\Framework\App\Request\Http#000000002835a139000000005f2d50d6#) called at [vendor/magento/module-store/App/FrontController/Plugin/RequestPreprocessor.php:99]
#22 Magento\Store\App\FrontController\Plugin\RequestPreprocessor->aroundDispatch(&Magento\Framework\App\FrontController\Interceptor#000000002835a0db000000005f2d50d6#, &Closure#000000002835a076000000005f2d50d6#, &Magento\Framework\App\Request\Http#000000002835a139000000005f2d50d6#) called at [vendor/magento/framework/Interception/Interceptor.php:135]
#23 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(&Magento\Framework\App\Request\Http#000000002835a139000000005f2d50d6#) called at [vendor/magento/module-page-cache/Model/App/FrontController/BuiltinPlugin.php:73]
#24 Magento\PageCache\Model\App\FrontController\BuiltinPlugin->aroundDispatch(&Magento\Framework\App\FrontController\Interceptor#000000002835a0db000000005f2d50d6#, &Closure#000000002835a076000000005f2d50d6#, &Magento\Framework\App\Request\Http#000000002835a139000000005f2d50d6#) called at [vendor/magento/framework/Interception/Interceptor.php:135]
#25 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(&Magento\Framework\App\Request\Http#000000002835a139000000005f2d50d6#) called at [app/code/Infortis/Cgen/Plugin/Magento/Framework/App/FrontController.php:32]
#26 Infortis\Cgen\Plugin\Magento\Framework\App\FrontController->aroundDispatch(&Magento\Framework\App\FrontController\Interceptor#000000002835a0db000000005f2d50d6#, &Closure#000000002835a076000000005f2d50d6#, &Magento\Framework\App\Request\Http#000000002835a139000000005f2d50d6#) called at [vendor/magento/framework/Interception/Interceptor.php:135]
#27 Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(&Magento\Framework\App\Request\Http#000000002835a139000000005f2d50d6#) called at [vendor/magento/framework/Interception/Interceptor.php:153]
#28 Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', array(&Magento\Framework\App\Request\Http#000000002835a139000000005f2d50d6#), array(array('mstCoreDispatch'))) called at [generated/code/Magento/Framework/App/FrontController/Interceptor.php:26]
#29 Magento\Framework\App\FrontController\Interceptor->dispatch(&Magento\Framework\App\Request\Http#000000002835a139000000005f2d50d6#) called at [vendor/magento/framework/App/Http.php:116]
#30 Magento\Framework\App\Http->launch() called at [generated/code/Magento/Framework/App/Http/Interceptor.php:24]
#31 Magento\Framework\App\Http\Interceptor->launch() called at [vendor/magento/framework/App/Bootstrap.php:261]
#32 Magento\Framework\App\Bootstrap->run(&Magento\Framework\App\Http\Interceptor#000000002835a12b000000005f2d50d6#) called at [index.php:39]
</pre>

Could anyone let me know what I have done wrong here? I'm guessing I have to declare the customer group value somewhere but I'm not sure how do do this.

Edit

Thanks to Marius This is now working. I just had to change the handler code to the below:

<?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');         
                }
             }
    }
}
Was it helpful?

Solution

you have this line if(customerGroup == 'ID/5'){
customerGroup does not have the variable sign $ in front of it.
But even if you add it, you will get an error because the variable is not defined.
I guess you want something like

$customerGroup = $this->customerSession->getCustomer()->getGroupId();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top