2

How to change the bestseller report items number more than 5 items Magento 2?

Thank In Advance..!

1
  • Are you trying to display best seller products using CMS Page? Commented Nov 29, 2019 at 11:00

2 Answers 2

3

It's very simple, you just need to add below code in your di.xml file here in your custom module

app/code/Vendor/Module/etc/di.xml

Content for this file is..

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection" type="Vendor\Module\Model\ResourceModel\Report\Bestsellers\Collection" />
</config>

Now create Block file here

app/code/Vendor/Module/Model/ResourceModel/Report/Bestsellers/Collection.php

Content for this file is..

<?php

namespace Vendor\Module\Model\ResourceModel\Report\Bestsellers;

class Collection extends \Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection
{
    protected function _construct()
    {
        parent::_construct();
        $this->_ratingLimit = 15; //change Default Limit Number here as you want
    }
}

Just set Default Limit in __construct() and run below commands

php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:clean
php bin/magento cache:flush

Hope this will help you!

7
  • Let me know if it helps. Thanks. Commented Nov 29, 2019 at 11:08
  • Thanks but I need to use the bestseller collection in another place not at the backend dashboard tabs Commented Nov 29, 2019 at 11:29
  • Where you want that ? Let me know I'll check that. Commented Nov 29, 2019 at 11:30
  • I need to show theme at the frontend and just I am getting the bestseller product ids and i using them in another collection Commented Nov 29, 2019 at 11:56
  • Okay let me check and get back to you Commented Nov 29, 2019 at 11:59
1

You need to override \Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection class in your custom module and set $_ratingLimit as per your requirement. Please check the below code for your reference.

Vendor\Module\Model\ResourceModel\Report\Bestsellers\Collection.php

<?php
namespace Vendor\Module\Model\ResourceModel\Report\Bestsellers;

class Collection extends \Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection {

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

}

Vendor\Modul\etc\adminhtml\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection" type="Vendor\Module\Model\ResourceModel\Report\Bestsellers\Collection" />
</config>

Edit:

After adding these files in your custom module, please run below commands:

php bin/magento s:up
php bin/magento s:d:c
php bin/magento s:s:d
php bin/magento c:f

Hope it helps!!!

5
  • Thanks, I added it but it still gets 5 items Commented Nov 29, 2019 at 10:54
  • Have you run the post-deployment commands as well? I've used the same code on a website and it is working fine.
    – Sumit
    Commented Nov 29, 2019 at 10:55
  • post deployment ? static deploy ! Commented Nov 29, 2019 at 11:58
  • I updated my answer for the same. Please check.
    – Sumit
    Commented Nov 29, 2019 at 12:19
  • 1
    Thanks, the problem that I had di.xml file under etc directory directly and I added a new one under adminhtml directory, but when I added the reference to the under etc directly one, the code worked Commented Dec 2, 2019 at 8:13

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.