[Solved] Magento 2 admin order grid model filters based on conditions

Ramesh KR Asks: 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" />

This override is working fine!

My class:

 * 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');
       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'); 

       $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.

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.