Magento – Magento 2 Best Seller Collection Limit

best-sellermagento2product-collection

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?

Best Answer

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