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

문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top