magento2 - 在 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');
}
}
}
}
解决方案
您可以使用 Observers 来做到这一点,这里有一个示例模块供您使用。该模块将在客户登录系统后更改商店ID。
- 在Vendor/Module/中创建registration.php文件
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);
- 在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>
- 在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>
- 在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');
}
}
}
推荐阅读
- java - 为什么我的 Grpc ClientInterceptor 没有正确的反应器上下文?
- googlebot - 谷歌搜索控制台报告奇怪的 404——这是谷歌机器人的错误吗?
- jenkins - 在 Jenkins 管道 Groovy 中,如何在 YAML 文件中将值设置为带引号的字符串?
- python - dataframe.groupby.agg TypeError: _agg_2dim() 接受 1 个位置参数,但给出了 2 个
- coinbase-api - {“消息”:“无效签名”} Coinbase Pro API
- r - 用向量改变一个列,该列采用数据帧中行的点积
- swiftui - VideoPlayer 视图防止 SwiftUI 导航栏隐藏
- html - 居中的徽标以及第二行中的导航项目
- google-apps-script - 如何使用谷歌应用脚本在特定范围内从工作表中获取特定图表并将其粘贴到另一张工作表上?
- hive - 如何关闭 Hive 事务