Magento 2 : How to get the best seller product collection?
-
18-02-2021 - |
Question
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 ?
La solution
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());
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange