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).

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;
}
| improve this answer | |
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;
| improve this answer | |
  • 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 Dec 20 '16 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. – zed Blackbeard Jan 24 '17 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());
}
| improve this answer | |
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());
}
| improve this answer | |
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.

| improve this answer | |

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.