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.

  • Hi @shyam did you find the solution? – vedu May 16 '18 at 4:45
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;
    }

}
| improve this answer | |
  • 1
    its not working i tried – shyam Sep 22 '17 at 13:14
  • 2
    I need recently viewed products array in Magento2 – shyam Sep 22 '17 at 13:21
  • please try widgate – Divyesh Sep 22 '17 at 13:24
  • if any problems with thi code..give me updat. – Divyesh Oct 3 '17 at 4:30
  • @Divyesh Does this work for in Magento > 2.2 ? It sometimes works and sometimes doesn't for me. See this issue ? – Sarasranglt Jan 14 '19 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
}
| improve this answer | |
  • Hi @rohan can we fetch collection by customer id? – Nagaraju K Dec 4 '18 at 5:32
  • Yes. You can refer this answer => magento.stackexchange.com/a/251228/51810 – Rohan Hapani Dec 4 '18 at 5:41
  • thanks for reply but where we need to add customer id for filtering? – Nagaraju K Dec 4 '18 at 6:27
  • It will automatically return collection as you logging user or guest. – Rohan Hapani Dec 4 '18 at 6:40
  • with the above logic can we implement REST API? please advise – Nagaraju K Dec 4 '18 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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