Skip to main content
added 227 characters in body
Source Link
DJ Dev
  • 509
  • 2
  • 7

Create events.xml file

app/code/Vendor/Module/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <event name="controller_action_predispatch">
        <observer name="customer_controller_action_predispatch_event" instance="Vendor\Module\Observer\ForceCustomerLoginObserver" />
    </event>
</config>

app/code/Vendor/Module/Observer/ForceCustomerLoginObserver.php

    <?php
    namespace Vendor\Module\Observer;
    
    use Magento\Framework\Event\ObserverInterface;
    
    class ForceCustomerLoginObserver implements ObserverInterface
    {
        /**
         * @var \Magento\Framework\App\Response\RedirectInterface
         */
        protected $redirect;
    
        /**
         * Customer session
         *
         * @var \Magento\Customer\Model\Session
         */
        protected $_customerSession;
    
        public function __construct(
            \Magento\Customer\Model\Session $customerSession,
            \Magento\Framework\App\Response\RedirectInterface $redirect
    
        ) {
    
            $this->_customerSession = $customerSession;
            $this->redirect = $redirect;
    
        }
    
        public function execute(\Magento\Framework\Event\Observer $observer)
        {
            $actionName = $observer->getEvent()->getRequest()->getFullActionName();

$controller = $observer->getControllerAction();
    
            if($actionName == 'catalog_product_view')
            {
                if(!$this->_customerSession->isLoggedIn()) {
                    $this->redirect->redirect($controller->getResponse(), 'customer/account/login');
                }
            }
        }
    }

After that don't forgot to clean cache !!

Create events.xml file

app/code/Vendor/Module/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <event name="controller_action_predispatch">
        <observer name="customer_controller_action_predispatch_event" instance="Vendor\Module\Observer\ForceCustomerLoginObserver" />
    </event>
</config>

app/code/Vendor/Module/Observer/ForceCustomerLoginObserver.php

<?php
namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class ForceCustomerLoginObserver implements ObserverInterface
{
    /**
     * @var \Magento\Framework\App\Response\RedirectInterface
     */
    protected $redirect;

    /**
     * Customer session
     *
     * @var \Magento\Customer\Model\Session
     */
    protected $_customerSession;

    public function __construct(
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\App\Response\RedirectInterface $redirect

    ) {

        $this->_customerSession = $customerSession;
        $this->redirect = $redirect;

    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $actionName = $observer->getEvent()->getRequest()->getFullActionName();

        if($actionName == 'catalog_product_view')
        {
            if(!$this->_customerSession->isLoggedIn()) {
                $this->redirect->redirect($controller->getResponse(), 'customer/account/login');
            }
        }
    }
}

After that don't forgot to clean cache !!

Create events.xml file

app/code/Vendor/Module/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <event name="controller_action_predispatch">
        <observer name="customer_controller_action_predispatch_event" instance="Vendor\Module\Observer\ForceCustomerLoginObserver" />
    </event>
</config>

app/code/Vendor/Module/Observer/ForceCustomerLoginObserver.php

    <?php
    namespace Vendor\Module\Observer;
    
    use Magento\Framework\Event\ObserverInterface;
    
    class ForceCustomerLoginObserver implements ObserverInterface
    {
        /**
         * @var \Magento\Framework\App\Response\RedirectInterface
         */
        protected $redirect;
    
        /**
         * Customer session
         *
         * @var \Magento\Customer\Model\Session
         */
        protected $_customerSession;
    
        public function __construct(
            \Magento\Customer\Model\Session $customerSession,
            \Magento\Framework\App\Response\RedirectInterface $redirect
    
        ) {
    
            $this->_customerSession = $customerSession;
            $this->redirect = $redirect;
    
        }
    
        public function execute(\Magento\Framework\Event\Observer $observer)
        {
            $actionName = $observer->getEvent()->getRequest()->getFullActionName();

$controller = $observer->getControllerAction();
    
            if($actionName == 'catalog_product_view')
            {
                if(!$this->_customerSession->isLoggedIn()) {
                    $this->redirect->redirect($controller->getResponse(), 'customer/account/login');
                }
            }
        }
    }

After that don't forgot to clean cache !!

Source Link
DJ Dev
  • 509
  • 2
  • 7

Create events.xml file

app/code/Vendor/Module/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <event name="controller_action_predispatch">
        <observer name="customer_controller_action_predispatch_event" instance="Vendor\Module\Observer\ForceCustomerLoginObserver" />
    </event>
</config>

app/code/Vendor/Module/Observer/ForceCustomerLoginObserver.php

<?php
namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class ForceCustomerLoginObserver implements ObserverInterface
{
    /**
     * @var \Magento\Framework\App\Response\RedirectInterface
     */
    protected $redirect;

    /**
     * Customer session
     *
     * @var \Magento\Customer\Model\Session
     */
    protected $_customerSession;

    public function __construct(
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\App\Response\RedirectInterface $redirect

    ) {

        $this->_customerSession = $customerSession;
        $this->redirect = $redirect;

    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $actionName = $observer->getEvent()->getRequest()->getFullActionName();

        if($actionName == 'catalog_product_view')
        {
            if(!$this->_customerSession->isLoggedIn()) {
                $this->redirect->redirect($controller->getResponse(), 'customer/account/login');
            }
        }
    }
}

After that don't forgot to clean cache !!