2

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.

1
  • Hi @shyam did you find the solution?
    – vedu
    Commented May 16, 2018 at 4:45

2 Answers 2

2
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;
    }

}
5
  • 1
    its not working i tried
    – shyam
    Commented Sep 22, 2017 at 13:14
  • 2
    I need recently viewed products array in Magento2
    – shyam
    Commented Sep 22, 2017 at 13:21
  • please try widgate
    – Divyesh
    Commented Sep 22, 2017 at 13:24
  • if any problems with thi code..give me updat.
    – Divyesh
    Commented Oct 3, 2017 at 4:30
  • @Divyesh Does this work for in Magento > 2.2 ? It sometimes works and sometimes doesn't for me. See this issue ? Commented Jan 14, 2019 at 18:42
1

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
}
5
  • Hi @rohan can we fetch collection by customer id? Commented Dec 4, 2018 at 5:32
  • Yes. You can refer this answer => magento.stackexchange.com/a/251228/51810 Commented Dec 4, 2018 at 5:41
  • thanks for reply but where we need to add customer id for filtering? Commented Dec 4, 2018 at 6:27
  • It will automatically return collection as you logging user or guest. Commented Dec 4, 2018 at 6:40
  • with the above logic can we implement REST API? please advise Commented Dec 4, 2018 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.