Magento 2 : get best seller product on daily basis
-
05-10-2020 - |
質問
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.
解決
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());
}
他のヒント
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.