My requirement is to get recently view product collection.
Below function is used in Magento1 :
$collection = Mage::getSingleton('Mage_Reports_Block_Product_Viewed')->getItemsCollection();
I want to use above collection in magento2.
My requirement is to get recently view product collection.
Below function is used in Magento1 :
$collection = Mage::getSingleton('Mage_Reports_Block_Product_Viewed')->getItemsCollection();
I want to use above collection in magento2.
namespace [Vendor]\RecentlyViewed\Block\Customer;
use \Magento\Catalog\Block\Product\Context;
use \Magento\Catalog\Model\Product\Visibility;
use \Magento\Customer\Model\Session as CustomerSession;
use \Magento\Reports\Block\Product\Viewed;
use \Magento\Reports\Model\Product\Index\Factory as IndexFactory;
class RecentlyViewed extends Viewed
{
/**
* @var \Magento\Customer\Model\Session
*/
private $customerSession;
/**
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Catalog\Block\Product\Context $context
* @param \Magento\Catalog\Model\Product\Visibility $productVisibility
* @param \Magento\Reports\Model\Product\Index\Factory $indexFactory
* @param array $data
*/
public function __construct
(
CustomerSession $customerSession,
Context $context,
Visibility $productVisibility,
IndexFactory $indexFactory,
array $data = [])
{
parent::__construct($context, $productVisibility, $indexFactory, $data);
$this->customerSession = $customerSession;
}
/**
* @return \Magento\Reports\Model\ResourceModel\Product\Index\Collection\AbstractCollection
*/
public function getRecentlyViewedProducts()
{
$collection = $this->getItemsCollection();
return $collection;
}
}
Try to use this code. It is useful for current user recent view product display :
protected $recentlyViewed;
public function __construct(
...
\Magento\Reports\Block\Product\Viewed $recentlyViewed
) {
...
$this->recentlyViewed = $recentlyViewed;
}
public function getMostRecentlyViewed(){
return $this->recentlyViewed->getItemsCollection();
// return recently viewed products for the customer
}