How to Show Recently Viewed Products Programmatically in Magento 2?
Today, we’re going to teach you guys how to show recently viewed products programmatically in your Magento 2 store.
The “Recently Viewed Products” section is extremely crucial & helpful as it encourages customers to make a purchase.
Displaying recently viewed products keeps customers from getting distracted and shows them which products they checked out earlier.
In simple words, a Recently Viewed Products section helps customers to quickly navigate back to what they saw earlier and purchase it.
This, in turn, can enhance the shopping experience for customers and increase your sales.
And in this post, we’re going to show you exactly how to show recently viewed programmatically in your Magento 2 store.
Step-by-Step Process to Show Recently Viewed Products Programmatically in Magento 2
Please follow the below steps to learn how to display recently viewed products programmatically in Magento 2.
Step – 1
First of all, create a catalog_product_view.xml file in your layout folder and paste the following code.
<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>
Step – 2
Now, save the file and check the output:
And that’s it! This is how easy it is to show recently viewed products programmatically in your Magento 2 store.
We hope that you have found this tutorial helpful. If you have any queries, please ask them in the comments below.
And if you need our professional assistance, feel free to contact us anytime.