Question

I want to remove all cancelled orders from admin sales_order_grid. I am trying to add a collection as below. But it's not updating. However the printed data showing correctly.

<?php
namespace Custom\Module\Model\ResourceModel\Order\Grid;

use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as OriginalCollection;
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Psr\Log\LoggerInterface as Logger;
class Collection extends OriginalCollection
{
    protected $_authSession;
    public function __construct(
       EntityFactory $entityFactory,
       Logger $logger,
       FetchStrategy $fetchStrategy,
       EventManager $eventManager,
       \Magento\Backend\Model\Auth\Session $authSession
      )
    {        
       $this->_authSession = $authSession;
       parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager);
    
    }

    protected function _initSelect()
    {
        parent::_initSelect();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
        $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
        $connection = $resource->getConnection();
        /*$sales_order = $resource->getTableName('sales_order'); 
        $sales_order_item = $resource->getTableName('sales_order_item'); */
        $sql ="SELECT * FROM `sales_order_grid` WHERE `status`!='canceled'"; 
        $result = $connection->fetchAll($sql); 
        return $result;
    }
}

If I print $result it's showing correct data, without cancelled orders. But in admin it's not updating.

Below is my di.xml code:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">   
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
    <arguments>
        <argument name="collections" xsi:type="array">
            <item name="sales_order_grid_data_source" xsi:type="string">Custom\Module\Model\ResourceModel\Order\Grid\Collection</item>
        </argument>
    </arguments>
</type>
Was it helpful?

Solution

app/code/VendorName/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">
    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="sales_order_grid_data_source" xsi:type="string">VendorName\ModuleName\Model\ResourceModel\Order\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
</config>

app/code/VendorName/ModuleName/Model/ResourceModel/Order/Grid/Collection.php

<?php

namespace VendorName\ModuleName\Model\ResourceModel\Order\Grid;

class Collection extends \Magento\Sales\Model\ResourceModel\Order\Grid\Collection
{
    protected function _renderFiltersBefore()
    {
        $this->getSelect()->where("main_table.status <> ?", \Magento\Sales\Model\Order::STATE_CANCELED);
        parent::_renderFiltersBefore();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top