How to Filter Order Grid by Multiple Order IDs in Magento 2
An organized and flexible order grid is the sign of an advanced E-commerce store. There’s no other alternative for managed order data when it comes to the order grid.
By default, Magento 2 order grid contains a number of columns for the ease of order processing. One can add a custom column in Magento 2 Order grid which is not available by default and add mass actions in the order grid in Magento 2 to get the things done quickly.
By default, one can filter data by single order ID.
In order to manage the order data in an efficient manner, it is required to expand features in the filtering. One of them is implementing multiselect filter for order status in Magento 2. Another essential addition is the ability to filter the order grid by multiple order IDs in Magento 2. You may require to filter the data by multiple order IDs to keep the track of some of the orders. Filtering them one by one becomes tedious. Thus, I’ve come up with the programmatic solution to filter order grid by multiple order IDs in Magento 2.
Steps to Filter Order Grid by Multiple Order IDs in Magento 2
1. Create di.xml at app\code\Vendor\Module\etc
1 2 3 4 5 6 7 8 9 |
<?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\FilterApplierInterface"> <plugin name="sales_order_grid_filter_modifier" type="Vendor\Module\Plugin\UiComponent\DataProvider\FilterApplier" sortOrder="1"/> </type> </config> |
2. Create FilterApplier.php file at app\code\Vendor\Module\Plugin\UiComponent\DataProvider
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php namespace Vendor\Module\Plugin\UiComponent\DataProvider; use Magento\Framework\Api\Filter; use Magento\Framework\App\Request\Http; use Magento\Framework\Data\Collection; use Magento\Framework\View\Element\UiComponent\DataProvider\FilterApplierInterface; use Magento\Sales\Api\Data\OrderInterface; class FilterApplier { const SALES_ORDER_GRID_NAMESPACE = 'sales_order_grid'; protected $request; public function __construct( Http $request ) { $this->request = $request; } public function beforeApply(FilterApplierInterface $subject, Collection $collection, Filter $filter) { $namespace = $this->request->getParam('namespace'); if ($namespace == self::SALES_ORDER_GRID_NAMESPACE) { if ($filter->getField() == OrderInterface::INCREMENT_ID) { $modifiedFilterValue = str_replace('%', '', $filter->getValue()); $modifiedFilterValue = preg_replace('/\s+/', '', $modifiedFilterValue); if (strpos($modifiedFilterValue, ",") !== false) { $filter->setValue($modifiedFilterValue); $filter->setConditionType('in'); } else { $filter->setValue('%' . $modifiedFilterValue . '%'); $filter->setConditionType('like'); } } } return [$collection, $filter]; } } |
Done! Start adding comma separated order IDs in the Magento 2 order grid to filter them together and track as shown below:
Quick enough, right?
If you have any doubts, just mention them in the Comments section below π I would be happy to help.
Feel free to share the solution with Magento 2 community via social media.
Thank You.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
2 Comments
Does this also works for a range of orders? Let’s say I want to filter on all orders between value order 100 and 110. You should fill out 100,101,102, etc. Or can you use 100,110, or what to use for a range as a value then?
Hey Ben , No it does not work for range of orders. So you cannot filter on all the orders.
Thank You!