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.

1

3 Answers 3

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

8
  • Did you means create a new module in app/code and create only etc and Observer Commented Jan 2, 2020 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
    Commented Jan 2, 2020 at 9:41
  • okay thanks, I will try it Commented Jan 2, 2020 at 9:42
  • Did you get any error?
    – DJ Dev
    Commented Jan 2, 2020 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", Commented Jan 2, 2020 at 10:47
1

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.

2
  • sorry abhinay I can not get it, please explain Commented Jan 2, 2020 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. Commented Jan 2, 2020 at 13:14
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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