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

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

2 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

3 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

4 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

6 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago