Skip to content

How to Create a Magento 2 Module for Displaying Recently Viewed Products in phtml?

I’m looking to create a Magento 2 module that displays recently viewed products on the storefront. I want to achieve something similar to the standard Magento widget (Last Viewed Products), but instead of using listing.html, I’d like to render the products directly in a custom phtml file.

Here are my questions:

  1. Which classes/models in Magento 2 should I use to fetch the list of recently viewed products?

  2. How can I retrieve this data and pass it to a block for use in the phtml template?

  3. Are there any best practices I should follow when implementing such a module?

  4. How can I handle caching to ensure the data updates dynamically as users browse?

I’d like a more custom solution rather than relying on built-in widgets to have complete control over the code in the phtml file.

Thanks in advance for any tips or guidance!