Magento – Magento 2 – Bestseller product collection

best-sellercollection;magento2product

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

Best Answer

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;
}