Magento 2: Change the bestseller report count more than 5 items
-
21-03-2021 - |
Question
How to change the bestseller report items number more than 5 items Magento 2?
Thank In Advance..!
Solution
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!
OTHER TIPS
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!!!