Magento 2 Get Recent Viewed Products Collection Programmatically

Displaying recently viewed products in Magento 2 is an effective way to use in Magento 2 store to attract customers to the products you want. It allows customers to keep an eye on all the products that they visited recently, which would help enhance the shopping experience of customers. It can be a good suggestion for them that encourages them to buy what they are likely to have interest in.

In order to display Recent Viewed Products collection on the Magento 2 site, store admin will need the data about Recent Viewed Products Collection. However, getting the collection is might be complex sometimes. Therefore, in today post, I will provide you three steps to get Recent Viewed Products Collection in Magento 2.

How to get Recent Viewed Products Collection in Magento 2

Step 1: Create RecentProducts block

To get Recent Viewed Products Collection, firstly, you need to create a RecentProducts block. To do that, follow the path Mageplaza/Productslider/Block/RecentProducts.php and add the below code:

<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_Productslider
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */
namespace Mageplaza\Productslider\Block;
use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\App\Http\Context as HttpContext;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Reports\Block\Product\Viewed as ReportProductViewed;
use Mageplaza\Productslider\Helper\Data;
/**
 * Class RecentProducts
 * @package Mageplaza\Productslider\Block
 */
class RecentProducts extends AbstractSlider
{
    /**
     * @var ReportProductViewed
     */
    protected $reportProductViewed;
    /**
     * RecentProducts constructor.
     * @param Context $context
     * @param CollectionFactory $productCollectionFactory
     * @param Visibility $catalogProductVisibility
     * @param DateTime $dateTime
     * @param Data $helperData
     * @param HttpContext $httpContext
     * @param ReportProductViewed $reportProductViewed
     * @param array $data
     */
    public function __construct(
        Context $context,
        CollectionFactory $productCollectionFactory,
        Visibility $catalogProductVisibility,
        DateTime $dateTime,
        Data $helperData,
        HttpContext $httpContext,
        ReportProductViewed $reportProductViewed,
        array $data = []
    ) {
        $this->reportProductViewed = $reportProductViewed;
        parent::__construct($context, $productCollectionFactory, $catalogProductVisibility, $dateTime, $helperData, $httpContext, $data);
    }
    /**
     * Get Collection Recently Viewed product
     * @return mixed
     */
    public function getProductCollection()
    {
        return $this->reportProductViewed->getItemsCollection()->setPageSize($this->getProductsCount());
    }
}

Step 2: Insert in phtml file

After having the collection in the block, now you can follow this snippet to get product collection from the block Mageplaza/HelloWorld/view/frontend/templates/list.phtml

Then, please insert the following code in the phtml file.

<?php
$collection = $block->getProductCollection();
foreach ($collection as $_product) {
    echo $product->getName() . ' - ' . $product->getProductUrl() . '<br />';
}

Step 3: Flush Cache & Test result

Finally, let’s flush cache and test result.

Conclusion

Above are three steps to help you get Recent Viewed Collection in Magento 2. I hope after reading this, you will be able to display your collection easily. If you have any questions or new ideas, feel free to leave a comment below.

Thanks for reading!

Enjoyed the tutorial? Spread it to your friends!

People also searched for

  • magento 2 get recently viewed products collection programmatically
  • magento 2 get recently viewed products collection
  • magento 2 get recently viewed products programmatically
  • magento 2 recently viewed products
  • recently viewed products magento 2
  • recently viewed magento 2
  • magento get recently viewed products collection
  • magento recently viewed products
  • show recently viewed products magento 2
  • magento 2 recently viewed products not showing
  • magento recently viewed
  • magento recently viewed products block
  • magento recently viewed products extension
  • recently viewed products magento
  • recently viewed products magento extension
  • recently viewed magento
  • recent viewed
  • recently viewed products
  • magento recently viewed products not showing
  • 2.2.x, 2.3.x, 2.4.x