How to add best seller products in home page custom slider. So, I need a best seller product collection in phtml file.
3
-
Similar question here : magento.stackexchange.com/questions/198708/… – Satish Dubariya Feb 13 '19 at 5:45
-
2Possible duplicate of How to get Best Selling Product Collection in Magento 2 using Object Manager – Prathap Gunasekaran Feb 13 '19 at 5:48
-
I tried same as your suggestion. But it will not working – Mayur Jotaniya Feb 13 '19 at 6:11
Add a comment
|
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