Magento – Add default filter to sales_order_grid for order status

filtermagento2magento2.3sales-order-griduicomponent

I want to filter the order grid in admin to see only the orders in status "processing" in the grid by default.

enter image description here

This grid is different from the others, I tried solutions from here but no success

In magento 1 I have done this with an observer on "controller_action_predispatch_adminhtml_sales_order_index"

Do you have any idea on how to do that ?

Thanks you

EDIT 1 : This is the code I used in Magento 1 to do this with the event:

<controller_action_predispatch_adminhtml_sales_order_index>
                <observers>
                    <set_default_filter_sales_order>
                        <type>singleton</type>
                        <class>Gone_PreparedGridFilter_Model_Adminhtml_Sales_Order_Observer</class>
                        <method>setDefaultGridFilter</method>
                    </set_default_filter_sales_order>
                </observers>
            </controller_action_predispatch_adminhtml_sales_order_index>

And in my file :

public function setDefaultGridFilter(Varien_Event_Observer $observer)
        {
            $request = Mage::app()->getFrontController()->getRequest();

            if (! $request->getParam('filter')) {
                    $filter = $this->_getDefaultEncodedFilterString(); //custom function
                    Mage::app()->getFrontController()
                        ->getRequest()
                        ->setParam('filter', $filter);
            }

            return $this;
        }

My goal is to have the grid filtered by the status when you arrived on it but to be able to modify all filter including the status one.

Edit 2

Does anyone have a solution for this, I'm still looking for a solution. I don't want to use the grid view save tool.

Best Answer

Update

For this use case there are two possible solutions

1. A simple non coding solution

Apply the Order Status Filter "Processing" in the Admin Order Grid then click on the "Default View" Tab and click the "Save view as" link, give the view a new name, say "Processing view" or any other name you wish. This will be set as a default view whenever you visit the order grid in subsequent page loads or admin login for the current user. The above solution works best if you have only single admin user.

Screenshot for your reference

https://prnt.sc/npo7hm

2. Solution via Custom Coding

After trying all other solutions listed in question links, i have derived the below solution for achieving the default filter for UI Component Admin Grid.

Override the sales_order_grid.xml in your custom module with the below codesnippet.

app/code/YourVendorPrefix/YourModuleName/view/adminhtml/ui_component/sales_order_grid.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<dataSource name="sales_order_grid_data_source" component="Magento_Ui/js/grid/provider">
    <settings>
        <filterUrlParams>
            <param name="status">processing</param>
        </filterUrlParams>
    </settings>
</dataSource>

The above xml code will filter the orders with the processing state. This will be applicable to all the admin users.