How to get Best Seller Products Collection in Magento 2?

In the fast-paced world of e-commerce, staying ahead of the curve is paramount. One way to ensure your Magento 2 store remains competitive is by curating a collection of best-selling products. These items not only drive sales but also enhance customer satisfaction and trust in your brand. In this guide, we'll delve into the strategies and techniques to effectively create a best-seller products collection in Magento 2.

Please follow the below steps to learn how to get Best Sellers product collection in your Magento 2 store.

Step 1: Firstly, we need to create a block named BestSellerProducts.php in the Mageefy/BestSeller/Block/ folder and paste the following code:

<?php
/**
 * @category   Mageefy
 * @package    Mageefy_BestSeller
 * @author     Mageefy Extension Team
 * @copyright  Copyright (c) Mageefy ( https://www.mageefy.com )
 * @license    https://www.mageefy.com/license-agreement
 */

namespace Mageefy\BestSeller\Block;
use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\App\Http\Context as HttpContext;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory as BestSellersCollectionFactory;
use Mageefy\BestSeller\Helper\Data;

/**
 * Class BestSellerProducts
 * @package Mageefy\BestSeller\Block
 */
class BestSellerProducts extends AbstractSlider
{
    /**
     * @var BestSellersCollectionFactory
     */
    protected $_bestSellersCollectionFactory;
    /**
     * BestSellerProducts constructor.
     * @param Context $context
     * @param CollectionFactory $productCollectionFactory
     * @param Visibility $catalogProductVisibility
     * @param DateTime $dateTime
     * @param Data $helperData
     * @param HttpContext $httpContext
     * @param BestSellersCollectionFactory $bestSellersCollectionFactory
     * @param array $data
     */
    public function __construct(
        Context $context,
        CollectionFactory $productCollectionFactory,
        Visibility $catalogProductVisibility,
        DateTime $dateTime,
        Data $helperData,
        HttpContext $httpContext,
        BestSellersCollectionFactory $bestSellersCollectionFactory,
        array $data = []
    ) {
        $this->_bestSellersCollectionFactory = $bestSellersCollectionFactory;
        parent::__construct($context, $productCollectionFactory, $catalogProductVisibility, $dateTime, $helperData, $httpContext, $data);
    }
    /**
     * get collection of best-seller products
     * @return mixed
     */
    public function getProductCollection()
    {
        $productIds = [];
        $bestSellers = $this->_bestSellersCollectionFactory->create()
            ->setPeriod('month'); //you can add period daily,yearly
        foreach ($bestSellers as $product) {
            $productIds[] = $product->getProductId();
        }
        $collection = $this->_productCollectionFactory->create()->addIdFilter($productIds);
        $collection->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents()
            ->addAttributeToSelect('*')
            ->addStoreFilter($this->getStoreId())->setPageSize($this->getProductsCount());
            
        return $collection;
    }
}

Step 2: After the above step insert the below code in phtml file at the following path and for this, create a list.phtml file in the Mageefy/BestSeller/view/frontend/templates/ folder and paste the following code:

<?php
$collection = $block->getProductCollection();

foreach ($collection as $_product) {
    echo $product->getName() . ' - ' . $product->getProductUrl() . '<br />';

}

Step 3: Flush Cache and view the result.

You can now easily display the bestseller products using the above steps. We hope this blog was helpful to you. If you have any queries, please feel free to leave a comment below.