首页 > 解决方案 > 在 Magento 2 中为不同的客户组自动更改商店视图

问题描述

我目前正在使用 Magento 2.3.2,我想根据他们的客户群向某些客户展示特定的商店视图。(例如,“General”组中的客户将看到默认的商店视图,而“Platinum”组中的客户将看到带有稍微不同的徽标和设计的“Platinum”商店视图)。

有没有可以做到这一点的扩展程序?我只能在目录中找到限制产品的那些?

编辑 20/02/2020 -

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

GroupSite/SiteSwitch/ 中的registration.php文件

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

GroupSite /SiteSwitch/etc/ 中的module.xml文件

<?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>

GroupSite / SiteSwitch /etc/frontend/中的 events.xml

<?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>

GroupSite/SiteSwitch/Observer 中的AddHandles.php文件

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

标签: magento2handlerstorecustomer

解决方案


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

  1. 在Vendor/Module/中创建registration.php文件
<?php
    \Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
    );
  1. 在Vendor/Module/etc/中创建module.xml文件
<?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. Vendor/Module/etc/frontend/中创建events.xml
<?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. Vendor/Module/Observer中为 Observer AddHandles.php文件创建处理程序文件
<?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');
        }
    }
}

推荐阅读