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

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…

4 hours ago

Best Beginners Guide to Shopify Balance Account

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

4 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…

4 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