Question

I need to filter the order grid by multiple order id's. So the when i enter comma separated order ids in the id filter it should filter by these ids and give result.

Was it helpful?

Solution

I just got one solution for this. I dont know it is the right way to implement the requirement. Any way i have done like below.

First i just created a before plugin for the filter apply function like below

<type name="Magento\Framework\View\Element\UiComponent\DataProvider\FilterApplierInterface">
    <plugin name="sales_order_grid_filter_modifier"
            type="Vendor\MyModule\Plugin\UiComponent\DataProvider\FilterApplier"
            sortOrder="1"/>
</type>

Then i have created the 'FilterApplier.php' file

<?php 
namespace Vendor\MyModule\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
 *
 * @package Vendor\MyModule\Plugin\UiComponent\DataProvider
 */
class FilterApplier
{

/**
 * Name space of the sales order grid that passes to the mui render call.
 */
const SALES_ORDER_GRID_NAMESPACE = 'sales_order_grid';

/**
 * @var Http
 */
protected $request;

/**
 * FilterApplier constructor.
 *
 * @param Http $request
 * @return void
 */
public function __construct(
    Http $request
) {
    $this->request = $request;
}

/**
 * This plugin will be executed before a filter is applied to the collection.
 * So each time a filter is applied in the sales order grid, we will check and if it is an ID filter
 * then we will change the condition of that filter from 'like' to 'in'. Also in the value there will be
 * %% symbols appended in the beginning and end of the value, we will remove that also.
 *
 * @param FilterApplierInterface $subject
 * @param Collection $collection
 * @param Filter $filter
 * @return array
 */
public function beforeApply(FilterApplierInterface $subject, Collection $collection, Filter $filter)
{
    // Get the namespace parameter from the request.
    // When we filter the grid, the request url will be mui/index/render.
    $namespace = $this->request->getParam('namespace');
    // Check it is sales_order_grid
    if ($namespace == self::SALES_ORDER_GRID_NAMESPACE) {
        // Out custom changes only applicable to the ID filter.
        if ($filter->getField() == OrderInterface::INCREMENT_ID) {
            // There will be % symbol in the start and end of the value. This is because the original
            // condition is like and for this they are preparing like this. We need to remove that.
            $modifiedFilterValue = str_replace('%', '', $filter->getValue());
            // Remove all the spaces in the comma separated string of id's
            $modifiedFilterValue = preg_replace('/\s+/', '', $modifiedFilterValue);
            $filter->setValue($modifiedFilterValue);
            // Change the condition to IN operator
            $filter->setConditionType('in');
        }
    }

    return [$collection, $filter];
}

}

Dont forget to change Vendor\MyModule with your vendor and module name

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top