---
title: "Magento 2: How to Get Most Viewed Product Collection"
url: "https://magecomp.com/blog/magento-2-get-viewed-product-collection/"
date: "2020-07-21T16:30:45+00:00"
modified: "2025-02-20T09:18:14+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "General"
  - "Magento 2"
word_count: 376
reading_time: "2 min read"
summary: "Hello Magento Folks?,"
description: "Most view products are one of the options that are provided by Magento 2. It helps the store in boosting sales. Learn How to Get Most Viewed Product Collection"
keywords: "General, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Fix &#8220;Uncaught Error: Class &#8216;Magento\Setup\Mvc\Bootstrap\InitParamListener&#8217; not found&#8221; Error After Upgrading to Magento 2.4.x?"
    url: "https://magecomp.com/blog/fix-class-magento-setup-mvc-bootstrap-initparamlistener-not-found-error/"
  - title: "How to Add Sign Out Tab in Customer Account in Magento 2?"
    url: "https://magecomp.com/blog/add-sign-out-tab-in-customer-account-magento-2/"
  - title: "How to Add Additional Options in Magento 2?"
    url: "https://magecomp.com/blog/add-additional-options-magento-2/"
---

# Magento 2: How to Get Most Viewed Product Collection

_Published: July 21, 2020_  
_Author: Dhiren Vasoya_  

![How to Get Most Viewed Product Collection in m2](https://magecomp.com/blog/wp-content/uploads/2020/07/How-to-Get-Most-Viewed-Product-Collection-in-m2-1024x512.webp)

**Hello Magento Folks?,**

How are you all working? Today I am going to help you all in How to Get Most Viewed Product Collection. In case you missed our previously placed article [**Magento 2: How to Get Best Seller Products Collection**](https://magecomp.com/blog/get-best-seller-products-collection-in-magento-2/).
Let’s Begin?
[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-Malware-Removal-CTA-4.webp)](https://magecomp.com/magento-malware-removal.html)

## Introduction:
There are many options provided by Magento for helping the users to choose the best items. Most view products are one of the options that are provided by Magento 2. It helps your Magento 2 store in improving sales. By displaying the Most Viewed Products in your store will help your customers to select the best product. But if you require to display the most viewed product you will have to get the most viewed product collection. Follow the below-given steps to get the Most Viewed Product Collection.
## Steps to getting Most Viewed Product Collection:
**Step 1:**  Create one block file on our custom extension
add Blockname.php in following path
**app\code\Vendor\Extension\Block\Blockname.php**

```
<?php

namespace VendorExtensionBlock;

use MagentoFrameworkViewElementTemplate;

class Blockname extends Template
{
    protected $_productsFactory;
    protected $_storeManager;

    public function __construct(
        MagentoFrameworkViewElementTemplateContext $context,
        MagentoReportsModelResourceModelProductCollectionFactory $productsFactory,
        MagentoStoreModelStoreManagerInterface $storeManager,
        array $data = []
    ) {
        $this->_productsFactory = $productsFactory;
	    $this->_storeManager = $storeManager;
        parent::__construct($context, $data);
    }

    public function getProductCollection()
    {
        $currentStoreId = $this->_storeManager->getStore()->getId();

        $collection = $this->_productsFactory->create()
                           ->addAttributeToSelect('*')
                           ->addViewsCount()
                           ->setStoreId($currentStoreId)
                           ->addStoreFilter($currentStoreId);

	    return $collection->getItems();
    }
}
```

[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-2.webp)](https://magecomp.com/magento-performance-optimization.html)

**Step 2:** After the above step Insert the below code in phtml file at the following path.
**app\code\Vendor\Extension\view\frontend\templates\list.phtml.**

```
<?php
$collection = $block->getProductCollection();
foreach ($collection as $_product) {
    echo $product->getName() . ' - ' . $product->getProductUrl() . '<br />';
}
```

**Step 3:** At last Flush Cache and view the result
## Final Words:
Most probably all are able to implement the above-given steps to Get Most Viewed Product Collection in Magento 2. But if you find any difficulties during the implementation then write down your queries in the comment section below I am solving there. Make sure you don’t forget to share the article with your developer friends. Till then stay safe and stay happy.
[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-4.webp)](https://magecomp.com/services/hyva-theme-development/)

**Happy Coding?**


---

_View the original post at: [https://magecomp.com/blog/magento-2-get-viewed-product-collection/](https://magecomp.com/blog/magento-2-get-viewed-product-collection/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-09 03:36:10 UTC_  
