0

I'm trying to extend model file from \Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection so I can change the $_ratingLimit = 10;

Based from example Overriding Block, Model, Controller in Magento2 What I have have so far
app/code/<Vendor>/<ModuleName>/etc/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="Vendorname\Modulename\Model\ResourceModel\Report\Bestsellers\Collection" />
</config>

And app/code/<Vendor>/<ModuleName>/Model/ResourceModel/Report/Bestsellers/Collection.php

<?php
namespace Vendorname\Modulename\Model\ResourceModel\Report\Bestsellers;

class Collection extends \Magento\Sales\Model\ResourceModel\Report\Collection\AbstractCollection
{
    protected $_ratingLimit = 10;   
}

Obvisously I am doing something or all wrong. I need help. Thank you.

  • check in var/log/system.log – xanka Mar 16 '16 at 3:36
  • I do find some errors, but after clearing the log file and removing above files. It generates exact identical errors. So, I dont think any of them are related to this. – crashtestxxx Mar 16 '16 at 4:04
  • 1
    lot of bug i there 1. you should not use preferences, use plugin if not at all possible then use preferences. 2. you should extend AbstractCollection, it should be Bestsellers/collection, – Pradeep Kumar Mar 16 '16 at 6:07
  • @PradeepKumar In my case, $_ratingLimit is non public variable, do I still able to use plug-in class? – crashtestxxx Mar 17 '16 at 1:29
  • Thanks everyone for suggestions. I was able to increase limit with the preference. – crashtestxxx Mar 18 '16 at 1:10
0

I know this is an old post, but it is the only post I could find online that was related to the issue I was having. I used this questions code as a starting point, but I couldn't understand what @Pradeep Kumar comment was referring to in regards to extending the class.

I finally figured out what they were referring to, so hopefully this helps someone else out that stumbles across this question.

I changed:

<?php
namespace Vendorname\Modulename\Model\ResourceModel\Report\Bestsellers;

class Collection extends \Magento\Sales\Model\ResourceModel\Report\Collection\AbstractCollection
{
    protected $_ratingLimit = 10;   
}

to:

<?php
namespace Vendorname\Modulename\Model\ResourceModel\Report\Bestsellers;

class Collection extends \Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection
{
  protected $_ratingLimit = 12;
}

The problem was that by extending \Magento\Sales\Model\ResourceModel\Report\Collection\AbstractCollection it was replacing the existing Collection class, but by extending \Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection you are basically editing the existing Collection class.

NOTE
Remember this code needs to be part of a custom module. [https://devdocs.magento.com/videos/fundamentals/create-a-new-module/]. That stumped me for a while :)

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.