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.

  • If I want to get collection of last 2 months then ? – Kushangi Panchal Apr 2 '18 at 12:19
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());
}
  • are there any differences from my answer? – Max Oct 26 '17 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. – Dinesh Yadav Oct 26 '17 at 15:19
  • @DineshYadav, can you provide the code with constructor rather than directly with objectManager – jafar pinjar Dec 21 '18 at 6:22
2

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.

  • Can you put full answer actually i may making some mistakes so i am not able to get output – Jaimin Oct 26 '17 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.