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.
- 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:
1 2 3 4 5 6 7 |
<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> |
Redirect straight to Product page on click of a category with single product in #Magento2 https://t.co/Nz1oxr6dsN
— MageComp (@theMageComp) February 15, 2016
2. then create the plugin class Block/Catalog/Product/ListProduct.php with the code below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<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.
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