在 Magento 2 中为不同的客户群自动更改商店视图

Change store view automatically for different customer groups in Magento 2

我目前使用的是 Magento 2.3.2,我想向某些客户展示基于他们的客户群的特定商店视图。 (例如,"General" 组中的客户会看到默认商店视图,而 "Platinum" 组中的客户会看到 "Platinum" 商店视图,其徽标和设计略有不同)。

是否有可以执行此操作的扩展程序?我只能在目录中找到限制产品的吗?

编辑 2020 年 2 月 -

感谢 Invigorate Systems 提供的解决方案。我现在已经在应用>代码文件夹中实现了如下代码:

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');         
                }
             }
    }
}

您可以使用 Observers 来做到这一点,这里有一个示例模块供您使用。此模块将在客户登录系统后更改商店ID。

  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');
        }
    }
}