5

I'm trying to get Best Seller Product collection using below code, but it will not display any data

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('\Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory');
$collection = $productCollection->create()->setModel('Magento\Catalog\Model\Product')->setPeriod('yearly');

How to get Best Seller Product collection ?

1
  • it might not working because you have set "yearly" instead of "year", please check my ans for more information. Commented Feb 13, 2019 at 9:51

2 Answers 2

8

Please try this code to get best seller product collections using objectManager.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Reports\Model\ResourceModel\Report\Collection\Factory'); 
$collection = $productCollection->create('Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection'); 

$collection->setPeriod('year');
//$collection->setPeriod('month');
//$collection->setPeriod('day');

foreach ($collection as $item) {
    print_r($item->getData());
}
3
  • Hi Aasim Goriya I have used same logic in my custom block but it is displaying empty records (those are not available currently at admin end products grid) could you please advise? Commented Apr 9, 2019 at 10:43
  • Exclude empty records using filters at code level Commented Apr 9, 2019 at 10:44
  • @AssimGoriya, How can i get latest month's best seller product? any Idea? Commented Apr 26, 2019 at 10:02
1

But if you want latest bestselling product collection for the day please use this code in block

public function getBestsellerProduct(){

$resourceCollection = $this->_resourceFactory->create('Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection');   
$resourceCollection->setPageSize(6);
$resourceCollection->setPeriod('day')->addStoreFilter(1)->getSelect()->order('period DESC');
return $resourceCollection;

}
1
  • 1
    same for latest month's best seller product instead of 'day' replace it by 'month' Commented Apr 2, 2021 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.