2

I am trying to fetch recently viewed products.

I tried to implement the provided code from here but did not get any success.

1
  • have u found a solution yet ? Commented Jan 14, 2019 at 18:44

4 Answers 4

3

Try below code in your template file for get recently viewed products in any place,

<?php echo $this->getLayout()
            ->createBlock("Magento\Reports\Block\Product\Widget\Viewed")
            ->setDisplayType("recently.view.products")
            ->setProductsCount("6")
            ->setTemplate("widget/viewed/content/viewed_list.phtml")
            ->toHtml(); 
 ?>

You can change setProductsCount() as you need.

4
  • Thank you for your answer @Nikunj Vadariya but it is not working. It is not displaying anything. Can I get recently viewed product collection programmatically?
    – vedu
    Commented May 16, 2018 at 6:10
  • do you want to display in product detail page? Commented May 16, 2018 at 6:19
  • 2
    No, I want to display at multiple places. Basically, I want recently viewed products collection.
    – vedu
    Commented May 16, 2018 at 6:31
  • not working in magento 2.3.5 Commented Sep 28, 2020 at 6:27
0

With this custom code, you can also get a most viewed products

  public function __construct(
        \Magento\Reports\Model\ResourceModel\Product\CollectionFactory $reportCollectionFactory,   
        \Magento\Store\Model\StoreManagerInterface $storeManager   

    ){
        $this->reportCollectionFactory = $reportCollectionFactory;
        $this->storeManager = $storeManager;
    }


public function getMostViewedProducts()
{
      $storeId =  $this->storeManager->getStore()->getId();

      $collection = $this->reportCollectionFactory->create()
            ->addAttributeToSelect(
                '*'
            )->addViewsCount()->setStoreId(
                    $storeId
            )->addStoreFilter(
                    $storeId
            );
      $items = $collection->getItems();
      return $items;
}
2
  • Thank you for your answer @Yogesh. Your code works fine but I need recently viewed products.
    – vedu
    Commented May 16, 2018 at 6:08
  • ohh.. sorry for misunderstanding...! Commented May 16, 2018 at 6:11
0

You can get recently view product by below code

protected $recentlyViewed;

public function __construct(
...
    \Magento\Reports\Block\Product\Viewed $recentlyViewed
) {
...
    $this->recentlyViewed = $recentlyViewed;
}

/**
 * Get recently viewed products for the customer
 *
 */
public function getMostRecentlyViewed(){
    return $this->recentlyViewed->getItemsCollection();
}
10
  • Thank you for the answer @Suresh Chikani but it is not returning anything.
    – vedu
    Commented May 16, 2018 at 8:51
  • It will be get collection after view any product. Commented May 16, 2018 at 9:00
  • I tried that also but still it returns nothing.
    – vedu
    Commented May 16, 2018 at 9:05
  • in which page do you showing products? Commented May 16, 2018 at 9:07
  • On category page.
    – vedu
    Commented May 16, 2018 at 9:18
0
protected $recentlyViewed;

public function __construct(
...
    \Magento\Reports\Block\Product\Viewed $recentlyViewed
) {
...
    $this->recentlyViewed = $recentlyViewed;
}

/**
 * Get recently viewed products for the customer
 *
 */
public function getMostRecentlyViewed(){

    return $this->recentlyViewed->getItemsCollection();

}

This code is working and it'll return according to customer session.

If you disabled Full Page cache then it'll display recently viewed prdocuts.

You need to set cacheable="false" for custom block. Please let me know if you still face same issue.

1
  • This looks like the code from this answer. If this answer adds something new, please properly format it. Commented Jul 10 at 12:51

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.