1

I just want to override vendor/magento/module-backend/Block/Dashboard/Orders/Grid.php I added this line $collection->addAttributeToFilter("status",["neq"=>"failed"]); Core overriding is successful but it doesn't effect admin dashboard.

<?php
/**
 * Created by PhpStorm.
 * User: development
 * Date: 19.01.2018
 * Time: 08:54
 */

namespace Example\OrderStatistics\Override;


use Magento\Backend\Block\Dashboard\Orders\Grid;

class PassFailedOrders extends Grid
{
    protected function _prepareCollection()
    {
        if (!$this->_moduleManager->isEnabled('Magento_Reports')) {
            return $this;
        }
        $collection = $this->_collectionFactory->create()->addItemCountExpr()->joinCustomerName(
            'customer'
        )->orderByCreatedAt();

        if ($this->getParam('store') || $this->getParam('website') || $this->getParam('group')) {
            if ($this->getParam('store')) {
                $collection->addAttributeToFilter('store_id', $this->getParam('store'));
            } elseif ($this->getParam('website')) {
                $storeIds = $this->_storeManager->getWebsite($this->getParam('website'))->getStoreIds();
                $collection->addAttributeToFilter('store_id', ['in' => $storeIds]);
            } elseif ($this->getParam('group')) {
                $storeIds = $this->_storeManager->getGroup($this->getParam('group'))->getStoreIds();
                $collection->addAttributeToFilter('store_id', ['in' => $storeIds]);
            }

            $collection->addRevenueToSelect();
        } else {
            $collection->addRevenueToSelect(true);
        }
        $collection->addAttributeToFilter("status",["neq"=>"failed"]); // This is added 

        $this->setCollection($collection);

        return parent::_prepareCollection();
    }

}

And here is my di.xml file.

<?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\Backend\Block\Dashboard\Orders\Grid" type="Example\OrderStatistics\Override\PassFailedOrders"/>
</config>

Any help will be appreciated. Thank you.

I found what is problem. in Magento\Backend\Block\Dashboard\Orders\Grid Class there is _prepareCollection() function. And when I override this function it just returns to again same class. But I need to return Magento\Backend\Block\Widget\Grid _prepareCollection() function. How do I ?

  • remove return parent::_prepareCollection(); from your function and return as return $this->setCollection($collection); – Rakesh Jesadiya Jan 19 '18 at 10:29
  • it doesn't work – Oğuz Can Sertel Jan 19 '18 at 10:36
0

Please use below code,

call parent::_prepareCollection($collection); before executing your code

use Magento\Backend\Block\Dashboard\Orders\Grid;

class PassFailedOrders extends Grid
{
    protected function _prepareCollection()
    {
    parent::_prepareCollection();

    if (!$this->_moduleManager->isEnabled('Magento_Reports')) {
        return $this;
    }
    $collection = $this->_collectionFactory->create()->addItemCountExpr()->joinCustomerName(
        'customer'
    )->orderByCreatedAt();

    if ($this->getParam('store') || $this->getParam('website') || $this->getParam('group')) {
        if ($this->getParam('store')) {
            $collection->addAttributeToFilter('store_id', $this->getParam('store'));
        } elseif ($this->getParam('website')) {
            $storeIds = $this->_storeManager->getWebsite($this->getParam('website'))->getStoreIds();
            $collection->addAttributeToFilter('store_id', ['in' => $storeIds]);
        } elseif ($this->getParam('group')) {
            $storeIds = $this->_storeManager->getGroup($this->getParam('group'))->getStoreIds();
            $collection->addAttributeToFilter('store_id', ['in' => $storeIds]);
        }

        $collection->addRevenueToSelect();
    } else {
        $collection->addRevenueToSelect(true);
    }
    $collection->addAttributeToFilter("status",["neq"=>"failed"]); // This is added 

    $this->setCollection($collection);

    return $this;
    }

}
| improve this answer | |

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.