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


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.

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