Site icon MageComp Blog

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

How to Redirect Category with Single Product to Product Page in Magento 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.

Exit mobile version