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?
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:
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.
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?
In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…
In the world of business, understanding financial metrics is crucial for making informed decisions and…
Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…
In this era, businesses are always on the lookout for ways to engage with their…
Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…
E-commerce has transformed the way consumers shop for products and services and interact with businesses.…
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.
How to Get Best Seller Products Collection using object manager
Why do you want Object Manager when complete code is provided here.