Categories: How ToMagento 2

How to Redirect Category with Single Product to Product Page in Magento 2

The most successful Ecommerce stores never fail to provide best user experience to their customers by any means. One of the most important aspect of it is to minify pageloads and avail customers with smoother navigation. This idea can be used in many cases. Let’s take an example of a category with single product. Redirecting customers to the category page and then to perform repeated click only to move to the product page may be useless in majority of situation. (It’s useful when a category describes some important information before visiting product page.) This illustration creates a need of finding the solution to redirect customers directly on product page in order to provide better navigation.

Let’s take a look at how we can achieve this in Magento 2. This requires developing a custom extension with the below code.

  1. As stated above, you need to create a small custom extension and modify the block’s method behavior for getting products collection.
    Create etc/di.xml in your extension with the following code:
<pre class="lang:default decode:true ">
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Block\Product\ListProduct">
    <plugin name="firstproduct-category-mod" type="Vendor\FirstProduct\Block\Catalog\Product\ListProduct" sortOrder="1"/>
</type>
</config>
</pre>

2. then create the plugin class Block/Catalog/Product/ListProduct.php with the code below:

<pre class="lang:default decode:true ">
<?php

namespace Vendor\FirstProduct\Block\Catalog\Product;


class ListProduct
{
    /**
     * @var \Magento\Framework\App\Response\Http
     */    protected $response;

    public function __construct(
       \Magento\Framework\App\Response\Http $response
    )
    {
        $this->response = $response;
    }

    /**
     * @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $resultCollection
     * @param \Magento\Catalog\Block\Product\ListProduct $subject
     * @return \Magento\Eav\Model\Entity\Collection\AbstractCollection $resultCollection
     */    public function afterGetLoadedProductCollection(\Magento\Catalog\Block\Product\ListProduct $subject, $resultCollection)
    {
        if ($resultCollection->count() == 1) {
            /** @var \Magento\Catalog\Model\Product $product */            $product = $resultCollection->getFirstItem();
            $this->response->setRedirect($product->getProductUrl());
        }

        return $resultCollection;
    }
}
</pre>

Clear the Magento default or other cache you are using.

You can also install the extension to implement the above functionality directly to your Magento store.

This extension is prepared using the code of  Yaroslav Rogoza done on this GitHub Repo. We are thankful to him for this.
Leave comment if you face any trouble with it.
Happy Coding.

Click to rate this post!
[Total: 8 Average: 4]
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.🏏

View Comments

Recent Posts

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

12 hours ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

4 days ago

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…

6 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…

6 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…

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

1 week ago