General

Magento 2: How to Get Best Seller Products Collection

Hello Magento pals?,

How are you? Hopefully, I am getting you safely. Our today’s tutorial topic is Magento 2 Get Best Sellers Collection. Let me tell you about our previous topic so you can check out How to Add Dynamic Link to Footer in Magento 2. Let’s Get Started?

Introduction:

Magento is considered one of the leading E-commerce platforms for online stores because of its advanced features. In that, the most interesting feature is Best Seller Products. The products which are sold in large numbers and that are top-rated that are recognized as BestSeller products. It helps the customers to make a more precise decision by viewing details about the best sellers collection. For displaying the best seller products follow the below-given steps:

Steps for Magento 2 Get Best Sellers Collection:

Step 1: Firstly, Create one block file on our custom extension
add Blockname.php in the following path

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

<?php

namespace Vendor\Extension\Block;

use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory as BestSellersCollectionFactory;
use Magento\Framework\View\Element\Template;
use Magento\Store\Model\StoreManagerInterface;

class Blockname extends Template
{
    protected $_bestSellersCollectionFactory;
    protected $_productCollectionFactory;
    protected $_storeManager;

    public function __construct(
        Context $context,
        CollectionFactory $productCollectionFactory,
        StoreManagerInterface $storeManager,
        BestSellersCollectionFactory $bestSellersCollectionFactory,
    ) {
        $this->_bestSellersCollectionFactory = $bestSellersCollectionFactory;
        $this->_storeManager = $storeManager;
        $this->_productCollectionFactory $productCollectionFactory;
     parent::__construct($context);
    }
   
    public function getProductCollection()
    {
        $productIds = [];
        $bestSellers = $this->_bestSellersCollectionFactory->create()
            ->setPeriod('month');
        foreach ($bestSellers as $product) {
            $productIds[] = $product->getProductId();
        }
        $collection = $this->_productCollectionFactory->create()->addIdFilter($productIds);
        $collection->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents()
            ->addAttributeToSelect('*')
            ->addStoreFilter($this->getStoreId())
            ->setPageSize(count($productIds));
        return $collection;
    }

    public function getStoreId(){
        return $this->_storeManager->getStore()->getId();
    }
}

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: Flush Cache and view the result. 

That’s It.

Final Words:

After the completion of the given above steps, you will be able to get the bestseller collection in Magento 2. In case any difficulties you face in the above-given steps then list down in the comment section below I will solve it for you. Make sure you don’t forget to share the article with your developer friends and help them. Till then stay home and stay safe.

Happy Coding?

Click to rate this post!
[Total: 11 Average: 4.3]
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.🏏

View Comments

  • This is an expert article you shared here. I got some unique and valuable information from your article. Thanks for sharing this article here.

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

2 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

4 days ago

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

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

4 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

5 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

6 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago