How to Get the Most Viewed Products Collection in Magento 2?

How to Get the Most Viewed Products Collection in Magento 2?

Every online store has its most viewed or popular products that make the most sales. Store owners can capitalize on these products by bringing them to the limelight. They can get a collection of their most viewed products and display them at a noticeable place to draw the user's attention.

The display of your most popular products have many advantages including customer engagement, sales improvement and consistent branding.

You can choose and display your most viewed products with a few clicks using our Magento 2 Product Slider Extension.

In Magento 2, it’s pretty simple to get the most viewed product collection. Following are the steps you can take to do so.

Step 1: Create a Block File

First, you need to create a block file (let’s call it BestSeller.php) in the following path.

app\code\Vendor\Extension\Block\BestSeller.php.

Add the following code in the file.

<?php
namespace FMEextensions\HelloWorld\Block;
class BestSeller extends \Magento\Framework\View\Element\Template
{
 
 protected $_collectionFactory;
 protected $_productsFactory;
 public function __construct(
 \Magento\Backend\Block\Template\Context $context,
 \Magento\Reports\Model\ResourceModel\Product\CollectionFactory $productsFactory,
 array $data = []
 ) {
 
 $this->_productsFactory = $productsFactory;
 parent::__construct($context, $data);
 }
public function getMostViewedData(){
 
 $mostViewedCollection = $this->_productsFactory->create()->addViewsCount(); 
 return $mostViewedCollection;
 }
}

Step 2: Show Output

Now insert the code shown below in the following phtml file to get product collection from block.

app\code\Vendor\Extension\view\frontend\templates\list.phtml.
<?php
 $mostViewedProduct = $block->getMostViewedData(); ?>
 <h1>Most Viewed Collection.....</h1>
 <ul>
 <?php foreach ($mostViewedProduct as $viewedProduct) {
 ?>
 <li><?php echo $viewedProduct>getSku();?>--<?php echo $viewedProduct>getViews();?></li>
 <?php } ?>
</ul>

Display Most Viewed Products Using Extension

An alternative to the above method is Magento 2 Product Slider Extension by FMEextensions. It allows you to add multiple sliders to display new, best selling, most viewed, or featured products anywhere on your product, category, and CMS pages. The extension is extremely easy to configure with a lot of useful features.

display most viewed products in magento 2

You get the following features when you opt for the extension.

  • Display more than one sliders
  • Display new products, best selling products, most viewed products, or featured products in each slider
  • Choose to display or hide out of stock products
  • Display sliders on selected products, categories or CMS pages
  • Display slider anywhere on a page or use the shortcode to carry it forward to any other page
  • Choose to display specific product attributes
  • Personalize slider (title, description, number of products to display)

If you have any issue in using Magento 2 then feel free to contact our Support team

. Other Articles You Might be Interested in: