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?
How to Get Best Seller Products Collection using object manager
Why do you want Object Manager when complete code is provided here.
This is an expert article you shared here. I got some unique and valuable information from your article. Thanks for sharing this article here.