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.
<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.
Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…
MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…
In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…
Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…
The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…
Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…
View Comments
Dose this support Magento 2.4.x ?
Yes it should work.
Will mess with breadcrumbs in Magento 2.3+
Thanks for the nice post, its very helpful for me.
Glad to hear :) Keep visiting for more such awesome posts,
It works fine on Magento 2.0!
Thank you very much, i was working on since two days… And no way !
Thanks for the Extension. It solved my problem