I'm currently trying to create a module for Magento 2 (2.3.4) which switches the store based on customer group using observers.
The code seems to work (uses the correct logo, header for the switched store) but unfortunately the different stores are supposed to use different payment methods and themes and for some reason the switched store still seems to pull it's theme and payment methods from the main site rather than it's parent site.
Ideally I need to change the website along with the store view. Is this possible or is there an alternative?
The code is 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' || $customerGroup === '6' || $customerGroup === '7'|| $customerGroup === '8'){
$this->_storeManager->setCurrentStore('14'); //Set your desired store ID that you wish to set.
}
else{
$this->_storeManager->setCurrentStore('1');
}
}
}
}
Can anyone suggest a solution please?