Zend certified PHP/Magento developer

Magento 2 admin order grid model filters based on conditions

I have overridden Magento’s default order grid controller. I’m trying to apply different filters based on the custom cookie I set.

My 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="MagentoSalesModelResourceModelOrderGridCollection"   type="SalesfilterGridModelResourceModelOrderGridCollection" />
</config>

This override is working fine!

My class:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace SalesfilterGridModelResourceModelOrderGrid;

use MagentoFrameworkDataCollectionDbFetchStrategyInterface as FetchStrategy;
use MagentoFrameworkDataCollectionEntityFactoryInterface as EntityFactory;
use MagentoFrameworkEventManagerInterface as EventManager;
use PsrLogLoggerInterface as Logger;

/**
 * Order grid collection
 */
class Collection extends MagentoFrameworkViewElementUiComponentDataProviderSearchResult
{
    /**
     * Initialize dependencies.
     *
     * @param EntityFactory $entityFactory
     * @param Logger $logger
     * @param FetchStrategy $fetchStrategy
     * @param EventManager $eventManager
     * @param string $mainTable
     * @param string $resourceModel
     */
    public function __construct(
        EntityFactory $entityFactory,
        Logger $logger,
        FetchStrategy $fetchStrategy,
        EventManager $eventManager,
        $mainTable = 'sales_order_grid',
        $resourceModel = MagentoSalesModelResourceModelOrder::class
    ) {
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
    }

    /**
     * @inheritdoc
     */
    protected function _initSelect()
    {
       $objectManager = MagentoFrameworkAppObjectManager::getInstance();  
       $request = $objectManager->get('MagentoFrameworkAppRequestHttp');  
       $param = $request->getParam('allorder');
       parent::_initSelect();
       if(isset($_COOKIE['Allorders_cookie'])) {
       $tableDescription = $this->getConnection()->describeTable($this->getMainTable());
       foreach ($tableDescription as $columnInfo) {
           $this->addFilterToMap($columnInfo['COLUMN_NAME'], 'main_table.' . $columnInfo['COLUMN_NAME']);
           $this->addFilterToMap('status', 'main_table.status'); 
           $this->addFieldToFilter('status',['processing']);  
 
       }

        }else{
       $allorders = $this->getConnection()->describeTable($this->getMainTable());
       foreach ($allorders as $allorder) {
           $this->addFilterToMap($allorder['COLUMN_NAME'], 'main_table.' . $allorder['COLUMN_NAME']);
           $this->addFilterToMap('status', 'main_table.status');  
           $this->addFieldToFilter('status', array('in' => array('processing','pending')));

       }

        }
       return $this;

    }
}

The cookie check condition is working but the collection didn’t re-initiate when another condition is satisfied. It seems like the addFieldToFilter is stuck and again not applying for the whole collection.
If the page is loaded with cookie first then the condition is satisfied and loaded. Again if the cookie is unset the other else is not working, it is not re-initiating the collection.