General

Magento 2: How to Get Most Viewed Product Collection

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.

Let’s Begin?

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 Vendor\Extension\Block;


use Magento\Framework\View\Element\Template;

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

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Reports\Model\ResourceModel\Product\CollectionFactory $productsFactory,
        \Magento\Store\Model\StoreManagerInterface $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();
    }
}

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.

Happy Coding?

Click to rate this post!
[Total: 10 Average: 4.5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

5 hours ago

Best Beginners Guide to Shopify Balance Account

If you are a Shopify admin, using a Shopify Balance Account for your business revenue…

5 hours ago

8 Best Social Login Apps for Shopify Store in 2024

Running an eCommerce business can be incredibly demanding, leaving entrepreneurs little time to focus on…

5 hours ago

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

1 day ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

2 days ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

2 days ago