1

Magento 2 Best Seller Collection:

<?php

namespace MPS\Allproduct\Block\Index;


class Index extends \Magento\Framework\View\Element\Template {


    protected $_coreRegistry = null;
    protected $_collectionFactory;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
       \Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory,
        array $data = []
    ) {
        $this->_collectionFactory = $collectionFactory;
        $this->_coreRegistry = $registry;
        parent::__construct($context, $data);
    }



    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
    public function getBestSellerData()
    {
        $collection = $this->_collectionFactory->create()->setModel(
            'Magento\Catalog\Model\Product'
        );

        return $collection;
    }       

}

I used above collection but it always show 5 products only but I don't know how it will work.

How to reset count. In My Collection is correct?

1 Answer 1

1

Take a look: vendor/magento/module-sales/Model/ResourceModel/Report/Bestsellers/Collection.php. We can see the limit:

/**
 * Rating limit
 *
 * @var int
 */
protected $_ratingLimit = 5;

We can try to reset the default limit, and then set the new one:

$dataCollection = $collection->getSelect();
$dataCollection->reset(\Magento\Framework\DB\Select::LIMIT_COUNT);
$dataCollection->limit(20); // Set custom limit
5
  • Exception #0 (Zend_Db_Select_Exception): Unrecognized method 'count()'
    – my Test
    May 3, 2017 at 15:28
  • Seem that you're calling count() method some wherelse? May 3, 2017 at 15:33
  • i removed count still 5 record only
    – my Test
    May 3, 2017 at 15:35
  • We should return $dataCollection. May 3, 2017 at 15:37
  • Still same Exception #0 (Zend_Db_Select_Exception): Unrecognized method 'getSize()'
    – my Test
    May 3, 2017 at 15:38

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.