Magento 2 : How to get best seller product collection using object manager in home page custom slider?

magento.stackexchange https://magento.stackexchange.com/questions/261509

Pergunta

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

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top