0

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>

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:

Custom field in order grid with filter

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!

3
  • Did you get any further with the custom filtering rule on sales_order_grid. Driving me a bit mad
    – elfling
    Jan 18, 2021 at 0:41
  • @elfling Thank you for reminding me to post my solution. Check my answer below and give me feedback if it works for you. Jan 19, 2021 at 11:56
  • Thank you, I'll see how I get on with that. I'm hoping that I can set condition to be in and also nin and use arrays. Much appreciate you getting back to an old thread
    – elfling
    Jan 19, 2021 at 12:09

1 Answer 1

0

I managed to handle that problem with around plugin:

    <type name="Magento\Ui\Component\Filters\Type\Select">
        <plugin name="my_module_order_grid_apply_filter_to_my_column" type="My\Module\Plugin\ApplyFilterPlugin" sortOrder="1" />
    </type>

And here's a code snippet of that plugin:

/**
     * @param Select   $subject
     * @param callable $callback
     * @param array    $params
     *
     * @return void
     */
    public function aroundPrepare(Select $subject, callable $callback, ...$params): void
    {
        if ($subject->getName() !== 'my_column') {
            $callback(...$params);

            return;
        }

        $filterParams = $subject->getContext()->getFiltersParams();

        if (!isset($filterParams[$subject->getName()])) {
            $callback(...$params);

            return;
        }

        $value = (int)$filterParams[$subject->getName()];

        $shippingToFilter = $this->filterBuilder->setField('shipping_to');

        switch ($value) {
            case 1:
                $shippingToFilter->setConditionType('notnull');
                break;
            case 0:
                $shippingToFilter->setConditionType('null');
                break;
            case -1:
                $shippingToFilter->setValue($this->timezone->date(new \DateTime())->format('Y-m-d H:i:s'));
                $shippingToFilter->setConditionType('lt');
                break;
        }
        $subject->getContext()->getDataProvider()->addFilter($shippingToFilter->create());

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.