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

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

Suppose you have 100 products on offer in your online store. Do you think that each product contributes equally to the store’s revenue? The answer is no. Instead, every store falls in line with the 80-20 rule, or popularly known as the Pareto Principle. In the context of eCommerce, this rule states that 80 percent of the store’s revenue comes only from 20 percent of the products. Therefore, store owners must capitalise on these 20 percent products and ensure they are prominent across the website. One way to do is to set up the recently viewed products Magento 2 feature.

What is the Magento 2 Recently Viewed Products Feature?

It is simply a way to highlight the recently or most viewed products across various areas of the website, such as product pages, CMS pages, or category pages. While you may think that it serves no purpose, but it has countless benefits, including:

Insights

Firstly, by providing users a list of most recently viewed products, you provide them insights into what others are buying. This can compel them to buy the same product, thereby boosting your sales.

Engagement

Another reason to set up this feature is to boost website engagement. Suppose a user does not like the product they are viewing currently. If they simply close the tab, it will impact your store’s bounce rate and you will lose a potential lead. By displaying other products, you can attract their attention. They may click on related and most viewed products, thereby boosting chances of a successful sale.

Marketing

Lastly, the most recently viewed products Magento feature can prove helpful from the marketing perspective. Store owners can use these products to create product-specific marketing campaigns. This goes a long way in boosting your sales.

Setting Up Recently Viewed Products in Magento 2

There are different ways to set up this functionality. One is to use a Magento 2 recently viewed products extension. For instance, you can choose and display your most viewed products with a few clicks using our Magento 2 Product Slider Extension. The other method simply involves following the below steps:

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

As said earlier, you can use our Magento 2 Product Slider Extension

to display the most viewed products. It allows you to add multiple sliders to display new, bestselling, 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)

Conclusion

If you have any issue in using Magento 2 then feel free to contact our support team. They will identify and fix the issue right away.

Other Related Extensions You Might be Interested in:

This blog was created with FME's SEO-friendly blog