How To

How to Restrict Customer to Access Category Page and Redirect to Another Page in Magento 2

Hello Magento pals

I hope you all had a great weekend. So, Christmas is here, and we are all busy doing preparation for this festival of light and joy.We (MageComp) want to wish you a Merry Christmas in advance and Happy new joyful year 2020? 

In Last We have learn How to Create Custom Order Status and State Programmatically in Magento 2, Today We are going to learn how you can restrict visitors to access category pages and redirect them to another page. 

Generally, In default Magento2, every visitor or customer can access all the Magento frontend pages like category pages, product pages, etc.

But what if you want to restrict visitors and customers from accessing a particular page? and also, what if you want to restrict some pages and redirect visitors from that page to another page? 

That another page can be a customer login or particular cms page. 

E.g. : If you want to restrict visitors to access category page and redirect visitors to another page, then with the help of the below code you can achieve that. So let’s get to it.

  1. To do this, create di.xml file inside app\code\Vendor\Extension\etc\ folder and add below code:
<?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\Model\Category">
        <plugin name="category_page_restrict" type="Vendor\Extension\Plugin\Restrictcategory" sortOrder="1"/>
    </type>
</config>

      2. Next, Create Restrictcategory.php file inside app\code\Vendor\Extension\Plugin\ folder and add below code: 

<?php
namespace Vendor\Extension\Plugin;

class Restrictcategory
{
    protected $_url;
    protected $_responseFactory;
    public function __construct(
        \Magento\Framework\UrlInterface $url,
        \Magento\Framework\App\ResponseFactory $responseFactory
    ) {
        $this->_url = $url;
        $this->_responseFactory = $responseFactory;
    }

    public function afterGetIsActive(\Magento\Catalog\Model\Category $category)
    

        if($category->getData('store_id')==1)//here you change condition as per need
        {   
    // you can set any redirect url as per your requirement

            $customRedirectionUrl = $this->_url->getUrl('customer/account/login');
 
            $this->_responseFactory->create()->setRedirect($customRedirectionUrl)->sendResponse();
            exit;
        }
    }
}
?>
 
  1. After applying these codes, clean the cache and then refresh it.

So, using the above codes, you can restrict visitors and your customers to access the category pages and redirect them to another page you want. 

If you found this article helpful, then give it a thumbs-up and let us know your reviews in the comments below. Also, please share it with your Magento colleagues and friends. 

Lastly, if you had any problem while implementing these codes or you need any help, then you can contact our Support team here

Again, We want to wish you a Merry Christmas and also want to thank you for taking the time to read this article ??

Click to rate this post!
[Total: 8 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

6 days ago