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.
<block class="Magento\Catalog\Block\Widget\RecentlyViewed" name="recently_viewed"
<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>
The display will be:
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.