1

Hello I am trying to override below function in below class using di.xml. Below is my code not working:

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" type="Custom\Module\Model\ResourceModel\Report\Collection" />
</config>

Below is my model file:

<?php

namespace Custom\Module\Model\ResourceModel\Report;

class Collection extends \Magento\Reports\Model\ResourceModel\Report\Collection
{
    /**
     * From value
     *
     * @var \DateTime
     */
    protected $_from;

    /**
     * To value
     *
     * @var \DateTime
     */
    protected $_to;

    /**
     * Report period
     *
     * @var int
     */
    protected $_period;

    /**
     * Intervals
     *
     * @var int
     */
    protected $_intervals;

    /**
     * Intervals
     *
     * @var int
     */
    protected $_reports;

    /**
     * Page size
     *
     * @var int
     */
    protected $_pageSize;

    /**
     * Array of store ids
     *
     * @var array
     */
    protected $_storeIds;

    /**
     * Set the resource report collection class
     *
     * @var string
     */
    protected $_reportCollection = null;

    /**
     * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
     */
    protected $_localeDate;

    /**
     * @var \Magento\Reports\Model\ResourceModel\Report\Collection\Factory
     */
    protected $_collectionFactory;

    /**
     * @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
     * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
     * @param \Magento\Reports\Model\ResourceModel\Report\Collection\Factory $collectionFactory
     */
    public function __construct(
        \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
        \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
        \Magento\Reports\Model\ResourceModel\Report\Collection\Factory $collectionFactory
    ) {
        $this->_localeDate        = $localeDate;
        $this->_collectionFactory = $collectionFactory;

        parent::__construct($entityFactory);
    }

    public function getReports()
    {
        if (!$this->_reports) {
            $reports = [];
            foreach ($this->_getIntervals() as $interval) {
                $interval->setChildren($this->_getReport($interval->getStart(), $interval->getEnd()));
                if (count($interval->getChildren()) == 0) {
                    $interval->setIsEmpty(true);
                }
                $reports[] = $interval;
            }
            $this->_reports = $reports;
        }

        return $this->_reports;
    }
}
1
  • Did you fix this issue? Feb 7, 2020 at 11:20

3 Answers 3

0

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

9
  • did it work for you by removing generated/* ? Jan 9, 2020 at 13:26
  • yes always because Magento generates dynamically. Jan 9, 2020 at 13:27
  • not working.... Jan 9, 2020 at 13:45
  • check-in var/log file? Jan 9, 2020 at 13:58
  • No logs generated either. Jan 9, 2020 at 14:00
0

Replace Following code in your Custom\Module\Model\ResourceModel\Report\Collection.php

<?php

namespace Custom\Module\Model\ResourceModel\Report;

class Collection extends \Magento\Reports\Model\ResourceModel\Report\Collection
{
    public function __construct
    (
        \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
        \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
        \Magento\Reports\Model\ResourceModel\Report\Collection\Factory $collectionFactory
    ) 
    {
        $this->_localeDate = $localeDate;
        $this->_collectionFactory = $collectionFactory;

        parent::__construct($entityFactory, $localeDate, $collectionFactory);
    }

    public function getReports()
    {
        if (!$this->_reports) {
            $reports = [];
            foreach ($this->_getIntervals() as $interval) {
                $interval->setChildren($this->_getReport($interval->getStart(), $interval->getEnd()));
                if (count($interval->getChildren()) == 0) {
                    $interval->setIsEmpty(true);
                }
                $reports[] = $interval;
            }
            $this->_reports = $reports;
        }
        return $this->_reports;
    }
}

You just missed following line in __construct function.

parent::__construct($entityFactory, $localeDate, $collectionFactory);
9
  • Yes I have also tried this way, but still facing the issue. function getReports() not calling from my custom file. it always calling from magento's module Jan 9, 2020 at 13:32
  • i have checked this in fresh magento2.3.3 and its working properly, check if same class and same function is override by any other module
    – Hiren
    Jan 9, 2020 at 13:35
  • No other module is conflicting. can you please share screenshot so I can also get help from it. Jan 9, 2020 at 13:45
  • in which grid this function is call??
    – Hiren
    Jan 9, 2020 at 13:51
  • admin => Reports => Product Ordered Report => Select Date ranges and click submit. If you put die('called'); in your module, then it will break the page after submit. Try to put in Magent's default file, you'll get an idea. You'll check any report, will call the function. Jan 9, 2020 at 13:53
0

In my case. I overwrite class \Magento\Reports\Model\ResourceModel\Customer\Totals\Collection\Initial You can copy function public function getReports() to this class and test

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.