Изменить вид магазина автоматически для разных групп клиентов в Magento 2 - PullRequest
2 голосов
/ 13 февраля 2020

В настоящее время я использую Magento 2.3.2, и я хотел бы показать определенным клиентам определенный c вид магазина в зависимости от их группы клиентов. (Например, клиент в группе «Общие» будет видеть представление магазина по умолчанию, в то время как покупатель в группе «Платиновый» будет видеть представление магазина «Платиновый» с немного другим lo go и дизайном).

Есть ли расширение, которое может это сделать? Я могу найти только те, которые ограничивают продукты в каталоге?

Изменить 20/02/2020 -

Благодаря Invigorate Systems для решение. Теперь я реализовал код, как показано ниже в папке app> code:

регистрация. php файл внутри GroupSite / SiteSwitch /

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

модуль. xml файл внутри 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>

события. xml внутри 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 файл внутри 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');         
                }
             }
    }
}

1 Ответ

2 голосов
/ 19 февраля 2020

Вы можете сделать это с помощью Observers, вот пример модуля для вас. этот модуль изменит идентификатор магазина после входа клиента в систему.

  1. Создание регистрации. php файл внутри Поставщик / Модуль /
<?php
    \Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
    );
Создать модуль. xml файл внутри Поставщик / Модуль / 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>
Создание событий. xml внутри Поставщик / Модуль / 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>
Создать файл обработчика для 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');
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...