2

I am trying to fetch recently viewed products.

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

  • have u found a solution yet ? – Sarasranglt Jan 14 at 18:44
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.

  • 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 May 16 '18 at 6:10
  • do you want to display in product detail page? – Nikunj Vadariya May 16 '18 at 6:19
  • 2
    No, I want to display at multiple places. Basically, I want recently viewed products collection. – vedu May 16 '18 at 6:31
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;
}
  • Thank you for your answer @Yogesh. Your code works fine but I need recently viewed products. – vedu May 16 '18 at 6:08
  • ohh.. sorry for misunderstanding...! – Yogesh May 16 '18 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();
}
  • Thank you for the answer @Suresh Chikani but it is not returning anything. – vedu May 16 '18 at 8:51
  • It will be get collection after view any product. – Suresh Chikani May 16 '18 at 9:00
  • I tried that also but still it returns nothing. – vedu May 16 '18 at 9:05
  • in which page do you showing products? – Suresh Chikani May 16 '18 at 9:07
  • On category page. – vedu May 16 '18 at 9:18

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.