0

Here Bestseller Products Collection Not coming zero for current month or day because for this month or day we don't have any orders even it is using old records and showing some number of data in magento 2??

<?php
namespace Vendor\Module\Block;
class BestSeller extends \Magento\Framework\View\Element\Template
{ 
   protected $_collectionFactory;
   public function __construct(
       \Magento\Backend\Block\Template\Context $context,
       \Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory
   ) { 
       $this->_collectionFactory = $collectionFactory;
       parent::__construct($context, $data);
   }

    public function getBestSellerData(){

        $bestSellerProdcutCollection = $this->_collectionFactory->create()
                    ->setModel('Magento\Catalog\Model\Product')
                    ->setPeriod('day') //you can add period daily,yearly
                    ->setPageSize(8);            

        return $bestSellerProdcutCollection;

    }


}
1
  • i have updated the answer please check the same Commented Nov 19, 2018 at 12:25

1 Answer 1

0
<?php
namespace Vendor\Module\Block;
class BestSeller extends \Magento\Framework\View\Element\Template
{ 
   protected $_resourceFactory;

   public function __construct(
       \Magento\Backend\Block\Template\Context $context,
        \Magento\Reports\Model\ResourceModel\Report\Collection\Factory $resourceFactory
   ) { 
       $this->_resourceFactory = $resourceFactory;
       parent::__construct($context, $data);
   }

    public function getBestSellerData(){
       //implemented logic based on reports

        $bestSellerProdcutCollection = $this->_resourceFactory->create('Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection');
        $bestSellerProdcutCollection->setPageSize(8);

        echo count($bestSellerProdcutCollection);
        return $bestSellerProdcutCollection;

    }


}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.