How to Programmatically Display Recently Viewed Products in Magento 2
Have you ever brought an item on a recommendation from your friends or family? Or maybe a product recommendation pushed you to select it over some other product.
Well, for online stores, it comes as a “recently viewed products” section that encourages customers for conversion.
Displaying recently viewed products in an online store keeps the customer from distracting while browsing through your store. Optimize your sales funnel by showing your customers what they checked earlier.
A customer may keep checking other products and have difficulty in finding what they saw/liked earlier. However, a recently viewed product section helps them navigate back to what they saw earlier and purchase it.
Also, a visitor can compare the previously viewed products with new products to make an informed purchase decision. It enhances the shopping experience of your online store.
For Magento 2 store owners, one can add ‘recently viewed products’ from the admin panel as well as programmatically. You can configure it using the widget also to show recently viewed products in Magento 2.
Here, I’ve posted the method to programmatically display recently viewed products in Magento 2:
Method to Programmatically Display Recently Viewed Products in Magento 2
Use the below code in the catalog_product_view.xml in your layout folder.
1 2 3 4 5 6 7 8 9 10 11 |
<referenceContainer name="content"> <block class="Magento\Catalog\Block\Widget\RecentlyViewed" name="recently_viewed" template="Magento_Catalog::product/widget/viewed/grid.phtml" after="-"> <arguments> <argument name="uiComponent" xsi:type="string">widget_recently_viewed</argument> <argument name="page_size" xsi:type="number">4</argument> <argument name="show_attributes" xsi:type="string">name,image,price,learn_more</argument> <argument name="show_buttons" xsi:type="string">add_to_cart,add_to_compare,add_to_wishlist</argument> </arguments> </block> </referenceContainer> |
The display will be:
Done!
Leverage the benefit of encouraging purchases by using the above method!
Any doubts? Do mention them in the Comments section below.
I would be glad to help you out.
Also, please share the solution with Magento Community via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
4 Comments
Did you check grid.phtml?
And also I want only 4 product in column with owl carousel.
Hello Ashutosh,
You need to take the collection of the collection of recent products in grid.phtml file.
It requires lots of efforts. You need to apply custom steps
Thank You
How can I add Carousel to recently viewed products?
Hello,
You need to customize your grid.phtml to add carousel on it.
Thank You