{"id":1764,"date":"2021-05-11T10:30:05","date_gmt":"2021-05-11T10:30:05","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/programmatically-display-recently-viewed-products-in-magento-2\/"},"modified":"2025-07-16T17:52:35","modified_gmt":"2025-07-16T12:22:35","slug":"programmatically-display-recently-viewed-products-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/programmatically-display-recently-viewed-products-in-magento-2\/","title":{"rendered":"How to Programmatically Display Recently Viewed Products in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Well, for online stores, it comes as a \u201crecently viewed products\u201d section that encourages customers for conversion.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For\u00a0Magento 2\u00a0store owners, one can add \u2018recently viewed products\u2019 from the admin panel as well as programmatically. You can configure it using the widget also to\u00a0<a href=\"https:\/\/meetanshi.com\/blog\/show-recently-viewed-products-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">show recently viewed products in Magento 2<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here, I\u2019ve posted the method to&nbsp;<em><strong>programmatically display recently viewed products in Magento 2<\/strong><\/em>:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Programmatically Display Recently Viewed Products in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use the below code in the&nbsp;<strong>catalog_product_view.xml&nbsp;<\/strong>in your layout folder.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;referenceContainer name=\"content\">\n    &lt;block class=\"Magento\\Catalog\\Block\\Widget\\RecentlyViewed\" name=\"recently_viewed\"\n           template=\"Magento_Catalog::product\/widget\/viewed\/grid.phtml\" after=\"-\">\n        &lt;arguments>\n            &lt;argument name=\"uiComponent\" xsi:type=\"string\">widget_recently_viewed&lt;\/argument>\n            &lt;argument name=\"page_size\" xsi:type=\"number\">4&lt;\/argument>\n            &lt;argument name=\"show_attributes\" xsi:type=\"string\">name,image,price,learn_more&lt;\/argument>\n            &lt;argument name=\"show_buttons\" xsi:type=\"string\">add_to_cart,add_to_compare,add_to_wishlist&lt;\/argument>\n        &lt;\/arguments>\n    &lt;\/block>\n&lt;\/referenceContainer><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The display will be:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2021\/05\/Screenshot-at-May-4th-2021-10.57.26-am-1-1024x632.png\" alt=\"How to Add Custom Field Below Apply Discount Code in Magento 2 Cart Page\" class=\"wp-image-14767\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Done!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Leverage the benefit of encouraging purchases by using the above method!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, please share the solution with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1764","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1764"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1764\/revisions"}],"predecessor-version":[{"id":18084,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1764\/revisions\/18084"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}