Magento 2 Get Recent Viewed Products Collection Programmatically

Magento 2 Get Recent Viewed Products Collection Programmatically

Create Recent Products block
Directory

VendorName/ModuleName/Block/RecentProducts.php

Content for this file

<?php
namespace VendorName\ModuleName\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 VendorName\Productslider\Helper\Data;
/**
* Class RecentProducts
* @package VendorName\ModuleName\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());
}
}


Insert in phtml file
Directory

VendorName/ModuleName/view/frontend/templates/list.phtml

Content for this file

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

Flush Cache And Check Result
To flush or clean the cache, run the command:

php bin/magento cache:clean

 

 

If you are looking to hire a Magento programmer, visit Magento Development Agency.