1

I have a site in Magento 2.3!

My requirement is when the customer does not log in to my site so he can not see product view page.

they see only the product listing page.

and when he click product so redirect the customer login page.

So please help me asap!

Thanks.

2

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

  • Did you means create a new module in app/code and create only etc and Observer – Rohit Gohel Jan 2 at 9:38
  • If you have any existing module then you can create events.xml and ForceCustomerLoginObserver.php file in that module. If you want to create new module than just create a simple module and then create events.xml and ForceCustomerLoginObserver.php file in that module. You will have to only update Vendor/Module name with you module name. – DJ Dev Jan 2 at 9:41
  • okay thanks, I will try it – Rohit Gohel Jan 2 at 9:42
  • Did you get any error? – DJ Dev Jan 2 at 10:17
  • yes when i click product so give this error Undefined variable: controller in /app\/code\/Sm\/Magetheme\/Observer\/ForceCustomerLoginObserver.php on line 38", – Rohit Gohel Jan 2 at 10:47
1

You can achieve the requirement by creating a module with following steps:

Step 1: Create registration.php under

app/code/STech/LoginProduct/registration.php

with below content:

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'STech_LoginProduct',
    __DIR__
);

Step 2: Create module.xml under

app/code/STech/LoginProduct/etc/module.xml

with below content:

<?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="STech_LoginProduct" setup_version="0.0.1">
        <sequence>
            <module name="Magento_Backend"/>
             <module name="Magento_Catalog"/>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

Step 3: Create di.xml under

app/code/STech/LoginProduct/etc/frontend/di.xml

with below content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Controller\Product\View">
        <plugin disabled="false" name="STech_Plugin_Catalog_Controller_Product_View" sortOrder="10" type="STech\LoginProduct\Plugin\Catalog\Controller\Product\View"/>
    </type>
</config>

Step 4: Create View.php under

app/code/STech/LoginProduct/Plugin/Catalog/Controller/Product/View.php

with below content:

<?php
namespace STech\LoginProduct\Plugin\Catalog\Controller\Product;

class View
{
    protected $customerSession;

    protected $_urlInterface;

    public function __construct(
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\UrlInterface $urlInterface
    ){
        $this->customerSession = $customerSession;
        $this->_urlInterface = $urlInterface;
    }

    public function aroundExecute(
        \Magento\Catalog\Controller\Product\View $subject,
        \Closure $proceed
    ) {
        if (!$this->customerSession->isLoggedin()) {
            $this->customerSession->setAfterAuthUrl($this->_urlInterface->getCurrentUrl());
            $this->customerSession->authenticate();
        }

        return $proceed();
    }
}

Now run the below commands:

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento setup:static-content:deploy

Thats it!

0

Override the layout catalog_product_view.xml and add a new custom block and phtml file.

Create a custom block file and add code for getting customer sessions, if the session exists continue otherwise redirect from it.

For adding custom block check that reference Add custom block and for getting customer session check reference Customer Session.

  • sorry abhinay I can not get it, please explain – Rohit Gohel Jan 2 at 9:41
  • Override the layout and add your own block and phtml file inside that layout. And Inside that block add a function that return current customer is login or not. If the customer is logged in then he/she can able to access of product page otherwise redirect to another page with your custom message. – abhinay pratap singh Jan 2 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.