2

I want to get the recently viewed product collection directly from model without using block function.

Thanks.

  • Did you find solution for this? – Shoaib Munir May 15 '19 at 9:56
  • @Shoaib Munir updated my post – Mahi M May 15 '19 at 12:42
3

It can get Recently viewed product collection in magento2 without using its block

        $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $recentlyViewedClass = \Magento\Reports\Model\Product\Index\Viewed::class;
        $_recentlyViewedProductCollection = $_objectManager->create($recentlyViewedClass)->getCollection();
        $_recentlyViewedProductCollection->addAttributeToSelect('*')
                                        ->setPageSize(10) // load 10 products
                                        ->load();

        echo 'Recently Viewed Products Count  : ' . $_recentlyViewedProductCollection->getSize() . PHP_EOL;

        if($_recentlyViewedProductCollection->getSize() > 0 )   {
            foreach($_recentlyViewedProductCollection as $_product) {
                echo $_product->getSku() .' | '. $_product->getName() . PHP_EOL;
            }
        }
  • But its returning not only recently viewed product, also all products – Mahi M Feb 18 '19 at 14:55
  • I was looking for this solution to get collection from model instead of block. Thanks :) +1 – Shoaib Munir May 15 '19 at 10:35
1
use Magento\Reports\Model\Product\Index\Factory;
$model = $this->indexFactory->get('viewed');
        $collection = $model->getCollection()
            ->addAttributeToSelect('*')
            ->setCustomerId($customerId);


$collection->excludeProductIds($productId)
                ->addUrlRewrite()
                ->setPageSize(($noOfProductsToDisplay ? $noOfProductsToDisplay : 5))
                ->setCurPage(1);

            $collection->setAddedAtOrder()
                ->setVisibility($this->productVisibility->getVisibleInSiteIds());
  • Thanks Mano M. +1 :) – Shoaib Munir May 15 '19 at 15:20
  • i am still getting all products don't know why – Waqar Ali Jun 12 '19 at 7:42
0

Please try this code

<?php 
namespace PackageName\ModuleName\Block\RecentlyViewed;

class Demo extends \Magento\Framework\View\Element\Template
{
    protected $recentlyViewed;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Reports\Block\Product\Viewed $recentlyViewed,
        array $data = []
    ) {
        $this->recentlyViewed = $recentlyViewed;
        parent::__construct( $context, $data );
    }

    public function getMostRecentlyViewed(){
        return $this->recentlyViewed->getItemsCollection()->getData();
    }
}
  • Thanks . but i need to achieve this in api . so i want to get recently viewed product collection without using block function. – Mahi M Feb 19 '19 at 6:52

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.