0

I want to force login on category page i have use below reference
Ref: https://magento.stackexchange.com/a/188208/68695

Above reference link good work for product detail page but not category page.

1

Try this,

Add di.xml

<?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\Category\View">
    <plugin name="customplugin" type="Vendor\ModuleName\Plugin\CategoryView" sortOrder="10"/>
</type>
</config>

then add plugin before for your execute like below code

<?php 
namespace Vendor\ModuleName\Plugin;
class CategoryView
{      

 public function __construct(
    .....
    \Magento\Customer\Model\Session $customerSession,
    ....
 ) {
    ...
    $this->customerSession=$customerSession;
    ...
 } 
 public function beforeExecute($subject)
 {
   if(!$this->customerSession->isLoggedin()){
       $this->customerSession->authenticate();
   }
 }
 } 

Check if customer is logged in or not and based on that we can authenticate. I haven't tested yet.

EDIT :

Using preference :

Add this in your di.xml

<preference for="Magento\Catalog\Controller\Category\View" type="Vendor\ModuleName\Controller\Rewrite\Category\View" />

then add a controller in the below path

vendor/ModuleName/Controller/Rewrite/Category/View.php

then add the below code to View.php

    <?php
    namespace Vendor\Module\Controller\Rewrite\Product;
    use Magento\Framework\App\Action\HttpPostActionInterface;
    use Magento\Framework\App\Action\HttpGetActionInterface;
    use Magento\Catalog\Api\CategoryRepositoryInterface;
    use Magento\Catalog\Model\Layer\Resolver;
    use Magento\Framework\Exception\NoSuchEntityException;
    use Magento\Framework\View\Result\PageFactory;
    use Magento\Framework\App\Action\Action;
    class View extends \Magento\Catalog\Controller\Category\View
    {
           Protected $customerSession;

     public function __construct(
      \Magento\Framework\App\Action\Context $context,
      \Magento\Catalog\Model\Design $catalogDesign,
      \Magento\Catalog\Model\Session $catalogSession,
      \Magento\Framework\Registry $coreRegistry,
      \Magento\Store\Model\StoreManagerInterface $storeManager,
      \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator 
      $categoryUrlPathGenerator,
       PageFactory $resultPageFactory,
       \Magento\Framework\Controller\Result\ForwardFactory 
      $resultForwardFactory,
      Resolver $layerResolver,
      CategoryRepositoryInterface $categoryRepository
     ) {
        $this->_catalogDesign = $catalogDesign;
        $this->_catalogSession = $catalogSession;
        $this->_coreRegistry = $coreRegistry;
        $this->_storeManager = $storeManager;
        $this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
        $this->resultPageFactory = $resultPageFactory;
        $this->resultForwardFactory = $resultForwardFactory;
        $this->layerResolver = $layerResolver;
        $this->categoryRepository = $categoryRepository;
        $this->customerSession=$customerSession;
        parent::__construct($context,$catalogDesign,$coreRegistry,$storeManager,$categoryUrlPathGenerator,$resultPageFactory,$resultForwardFactory,$layerResolver,$categoryRepository);
     }
     public function execute()
     { 
        if(!$this->customerSession->isLoggedin()){
           $this->customerSession->authenticate();
        }
     }
     }

Hope this helps

4
  • any class not override? like reference link Jun 11 '19 at 7:05
  • Plugin is an interceptors which will work before the actual execute function triggers and there we can check for the current customer is logged in or not and redirect or authenticate like this Jun 11 '19 at 7:07
  • i don't think $this->customerSession->setAfterAuthUrl($this->_url->getCurrentUrl()); need
    – Amit Bera
    Jun 11 '19 at 7:21
  • Is that worked? Jun 11 '19 at 11:47

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.