3

There are some tables related to bestseller in Magento 2 and it is using following tables to get best product collection.

sales_bestsellers_aggregated_daily
sales_bestsellers_aggregated_monthly
sales_bestsellers_aggregated_yearly 

I think, magento2 use sales_bestsellers_aggregated_yearly table for best seller products. I am using \Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory and it is working fine.

I am getting the bestseller collection yearly basis. How can i achieve bestseller collection by monthly basis or daily basis.

Thanks in advance.

1
  • If I want to get collection of last 2 months then ? Apr 2, 2018 at 12:19

2 Answers 2

2

Try this

$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('month');
//$collection->setPeriod('year');
$collection->setPeriod('day');

foreach ($collection as $item) {
    print_r($item->getData());
}
3
  • are there any differences from my answer?
    – Max
    Oct 26, 2017 at 14:27
  • @Max There is no difference in terms of logic. You only posted the logic of setting intervals and I posted the complete code. That's it. Oct 26, 2017 at 15:19
  • @DineshYadav, can you provide the code with constructor rather than directly with objectManager Dec 21, 2018 at 6:22
3

You can use \Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection::setPeriod method for define aggregation interval.

/** @var \Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory */
$collection = $collectionFactory->create();
$collection->setPeriod('month');
//$collection->setPeriod('day');
//$collection->setPeriod('year');

Also you can look at Magento\Reports\Block\Adminhtml\Grid\AbstractGrid for clarify interaction with magento base Report Collections.

1
  • Can you put full answer actually i may making some mistakes so i am not able to get output
    – Jaimin
    Oct 26, 2017 at 5:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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