Magento 2 : How to get best seller product collection using object manager in home page custom slider?
-
18-02-2021 - |
문제
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.
제휴하지 않습니다 magento.stackexchange