0

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?

0

OK, basically the solution for me, was to give up on trying to use multiple stores on the same site and create a separate site in the sub directories using this guide:

https://gist.github.com/thagxt/0f605f0a8a95c79302db0d2f04383788

The example given is as follows:

Set up Magento 2 multiple websites in sub directories

  1. Go to Admin > Stores > All Stores
  2. Click > Create Web Site
  3. In the Name field, enter store name. e.g. Japan
  4. In the Code field, enter a unique string without spaces and > Save Web Site e.g. super_jp
  5. Create Store Create Store View Go to Stores > Configuration
  6. Select the website we just created. Go to Web & change both Base URLs & Base URLs (Secure)
e.g. Base URL:
    http://example.com/ Base Link URL: {{unsecure_base_url}}jp/ ... 
    same
    goes for Base URLs (Secure) Save. Clear Cache. Reindex.

Doing stuff in server root

Create a subfolder with the desired name. e.g. /jp/ Copy index.php & .htaccess from root (!important copy both files)

  1. Open index.php clear everything inside it and paste:
require realpath(__DIR__) . '/../app/bootstrap.php';
$params = $_SERVER;
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] = 'super_jp'; // change this with the code you chose in step. 4
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] = 'website'; // store or website
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);
$app = $bootstrap->createApplication('Magento\Framework\App\Http');
$bootstrap->run($app);

Save. Clear Cache. Reindex.

I now simply redirect a customer group to the other site and their settings take over from there.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.