1

I am trying to override this class

Magento\Reports\Model\ResourceModel\Report\Collection\AbstractCollection

This is a simple class not abstract class

Here is some code example of core class

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Reports\Model\ResourceModel\Report\Collection;

/**
 * Report collection abstract model
 *
 * @api
 * @since 100.0.2
 */
class AbstractCollection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
    /**
     * From date
     *
     * @var string
     */
    protected $_from = null;

    /**
     * To date
     *
     * @var string
     */
    protected $_to = null;

But when i am trying to override this class in my custom module than it is not overriding.

Here is the code which i am writing for override

app/code/MyVendor/MyModule/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\Reports\Model\ResourceModel\Report\Collection\AbstractCollection" type="MyVendor\MyModule\Rewrite\Magento\Reports\Model\ResourceModel\Report\Collection\AbstractCollection"/>
</config>

app/code/MyVendor/MyModule/Rewrite/Magento/Reports/Model/ResourceModel/Report/Collection/AbstractCollection.php

<?php
/**
 * Copyright ©  All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace MyVendor\MyModule\Rewrite\Magento\Reports\Model\ResourceModel\Report\Collection;

class AbstractCollection extends \Magento\Reports\Model\ResourceModel\Report\Collection\AbstractCollection
{
    protected function _applyStoresFilterToSelect(\Magento\Framework\DB\Select $select)
    {
        echo "i want to override this method"; die;
    }
}

After writing this code. Methods of that class are still showing content from the core class.

Any help will be appreciated.

2
  • 1
    You cannot override the Abstract Class
    – Amit Bera
    Oct 22, 2022 at 12:53
  • Check your custom and third party modules, same class is override or not in any modules. Di file is in etc, and no, it's not override by thrd party modules. Oct 22, 2022 at 13:26

2 Answers 2

1

I have the same error, fetch override another file so, You need to remove generated, var/* after the cache clean and check again.

Change _applyStoresFilterToSelec to _applyStoresFilter

app/code/MyVendor/MyModule/Rewrite/Magento/Reports/Model/ResourceModel/Report/Collection/AbstractCollection.php


 <?php
 declare(strict_types=1);

 namespace MyVendor\MyModule\Rewrite\Magento\Reports\Model\ResourceModel\Report\Collection;
 
 class AbstractCollection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
 {

     protected function _applyStoresFilter()
     {
         return $this->_applyStoresFilterToSelect($this->getSelect());
     }
  
 }
1

Magento\Reports\Model\ResourceModel\Report\Collection\AbstractCollection class does not instantiate by Object Manager, so you can not use preference to override this class.

Magento\Reports\Block\Adminhtml\Grid\AbstractGrid class has getCountTotals() method that create Collection based on resource collection name which extends Magento\Reports\Model\ResourceModel\Report\Collection\AbstractCollection. Eg:

  • Magento\Sales\Model\ResourceModel\Report\Order\Collection
  • Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection
  • Magento\Tax\Model\ResourceModel\Report\Collection
  • ...

So, to achieve your goal: override Magento\Reports\Model\ResourceModel\Report\Collection\AbstractCollection::_applyStoresFilter, you should determine which Grids you would like to implement, then override the corresponding Collection class that declare in getResourceCollectionName method of Grid class.

Eg, if you want to override Magento\Reports\Model\ResourceModel\Report\Collection\AbstractCollection::_applyStoresFilter in Bestsellers grid, you have to override Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection:

app/code/MyVendor/MyModule/etc/di.xml

<preference for="Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection" type="MyVendor\MyModule\Rewrite\Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection"/>

app/code/MyVendor/MyModule/Rewrite/Magento/Sales/Model/ResourceModel/Report/Bestsellers/Collection.php

<?php
declare(strict_types=1);

namespace MyVendor\MyModule\Rewrite\Magento\Sales\Model\ResourceModel\Report\Bestsellers;
 
class Collection extends \Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection
 {
     protected function _applyStoresFilterToSelect(\Magento\Framework\DB\Select $select)
     {
         echo 'you are now able to override the parent method'; die;
     }
 }

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.