I'm trying to add a new field to order grid with custom filter. Right now I managed to make it work, but I want to add my custom filtering rule.
I added the column to db_schema.xml and in my module's sales_order_grid.xml file I added:
<columns name="sales_order_columns">
<column name="custom_field">
<settings>
<filter>select</filter>
<options class="My\Module\Component\Listing\Columns\CustomFieldOptions" />
<dataType>select</dataType>
<label translate="true">Custom Field</label>
<visible>true</visible>
</settings>
</column>
</columns>
And in my CustomFieldOptions class I have:
<?php
declare(strict_types=1);
namespace My\Module\Component\Listing\Columns;
use Magento\Framework\Data\OptionSourceInterface;
class CustomFieldOptions implements OptionSourceInterface
{
/**
* @var string
*/
private const VALUE = 'value';
/**
* @var string
*/
private const LABEL = 'label';
/**
* Return array of options as value-label pairs
*
* @return array Format: array(array('value' => '<value>', 'label' => '<label>'), ...)
*/
public function toOptionArray() :array
{
return [
[
self::VALUE => 1,
self::LABEL => 'Realized'
],
[
self::VALUE => 0,
self::LABEL => 'Outdated'
],
[
self::VALUE => -1,
self::LABEL => 'Canceled'
]
];
}
}
My custom_field
is DateTime, but I want to filter it using those 3 CustomFieldOptions parameters:
I want to specify my own filtering rule (for example option Canceled
should filter only records with null
value of my custom_field
, Outdated
should return records with custom_field
value that's older than current day).
Can you help me with what should I do now? Thanks in advance!