4

I am trying to get bestseller products on homepage using product collection in Magento 2. I only got Most Viewed product listing (?) and I am not getting bestseller products.

Here is my code:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Reports\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create()
          ->addAttributeToSelect('*')
          ->addAttributeToFilter('status','1')
          ->setPageSize(8)
          ->load();

Can someone tell me which ResourceModel I need to load? I tried with:

Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory`enter code here`

But didn't helped (I got some error with that code and that resource).

2

6 Answers 6

3

Try code below :

protected $_collectionFactory;

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory,
    array $data = []
) {
    $this->_collectionFactory = $collectionFactory;
    parent::__construct($context, $data);
}
public function getBestSellerData(){
    $bestSellerProdcutCollection = $this->_collectionFactory->create()
        ->setModel('Magento\Catalog\Model\Product')
        ->setPeriod('month'); //you can add period daily,yearly
        //->setPeriod('year');
        //->setPeriod('day');

    return $bestSellerProdcutCollection;
}
2

M2 have some tables related to bestsell sales_bestsellers_aggregated_daily sales_bestsellers_aggregated_monthly sales_bestsellers_aggregated_yearly
In code you can filter bestsell by method joinField you can do this

$storeId = $this->_storeManager->getStore(true)->getId();
/** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
$collection = $this->productCollectionFactory->create()->addAttributeToSelect('*');
$collection->addStoreFilter()
        ->joinField(
            'qty_ordered',
            'sales_bestsellers_aggregated_monthly',
            'qty_ordered',
            'product_id=entity_id',
            'at_qty_ordered.store_id='.$storeId,
            'at_qty_ordered.qty_ordered > 0',
            'left'
        )->setPageSize(8)->setCurPage(1);
return $collection;
2
  • What i understand it's working in own module, it's doesn't work in theme custom block. I need add some referencje/attributes to this block?
    – Dominik
    Commented Dec 20, 2016 at 10:10
  • I think, Magento 2 using default sales_bestsellers_aggregated_yearly table to get bestseller product, How can get best seller products using sales_bestsellers_aggregated_daily table. Commented Jan 24, 2017 at 12:37
1

You can try this code in that you can also filter the product by Month,year or day .

$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());
}
1

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());
}
1

U can used this code and get the Bestseller Product,

$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());
}

after used this code , In order for best selling products to show up in your magento website go to Reports > Refresh Statistics select all items > "Refresh Lifetime Statistics" -> submit.

0
$productsCollection = $this->getProductsCollection();
$productsCollection->getSelect()
    ->joinLeft(
        'sales_order_item',
        'e.entity_id = sales_order_item.product_id and sales_order_item.store_id = ' . $store->getId() . ' and date(sales_order_item.created_at) >= (CURDATE() - INTERVAL '. $daysInterval .' DAY)',
        [
            'bestsellers' => 'SUM(sales_order_item.qty_ordered)'
        ]
    )
    ->group('e.entity_id')
    ->having('SUM(sales_order_item.qty_ordered) > 0')
    ->order('bestsellers desc');

return $productsCollection;

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.