2

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

Thank In Advance..!

  • Are you trying to display best seller products using CMS Page? – Sanjay Gohil Nov 29 '19 at 11:00
2

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!

| improve this answer | |
  • Let me know if it helps. Thanks. – Kishan Savaliya Nov 29 '19 at 11:08
  • Thanks but I need to use the bestseller collection in another place not at the backend dashboard tabs – Robert Baum Nov 29 '19 at 11:29
  • Where you want that ? Let me know I'll check that. – Kishan Savaliya Nov 29 '19 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 – Robert Baum Nov 29 '19 at 11:56
  • Okay let me check and get back to you – Kishan Savaliya Nov 29 '19 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!!!

| improve this answer | |
  • Thanks, I added it but it still gets 5 items – Robert Baum Nov 29 '19 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 Nov 29 '19 at 10:55
  • post deployment ? static deploy ! – Robert Baum Nov 29 '19 at 11:58
  • I updated my answer for the same. Please check. – Sumit Nov 29 '19 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 – Robert Baum Dec 2 '19 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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