0

How to add best seller products in home page custom slider. So, I need a best seller product collection in phtml file.

0

Use always construct method instead of oject manager. Add below code in your block file :

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 getBestSellerCollection() {
    $bestSellerProdcutCollection = $this->_collectionFactory->create()->setModel('Magento\Catalog\Model\Product')->setPeriod('month');        
    return $bestSellerProdcutCollection;
}

and add this code in phtml :

$bestSeller =  $block->getBestSellerCollection();

Hope, It will helpful for you.

  • how I can get best seller just like admin dashboard i.e without specify period ? – Pawan Nov 25 '20 at 11:37

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.