Webkul
Services
Technologies Bagisto QloApps
iOS Android SAP BigCommerce Wix
Trusted by
Samsung Intel Toyota Walmart Disney Nokia Nestlé Decathlon
See all customers
Back to Top
DEV

Add Custom Column Filter in Order Grid Collection in Magento 2

We’ll explore how to add a custom column’s filter to Magento 2 order grid collection, enabling more precise filtering based on fields you’ve added to your order tables.

For example, a custom_column field is created in the sales_order and sales_order_grid record tables in the database and we want to add the filter for the custom_column field when fetching the collection for the sales order grid.

Then, to achieve this functionality, please follow the below steps:

Step1. Create di.xml file inside the app/code/Vendor/Module/etc/ directory. Create preference class Vendor\Module\Model\ResourceModel\Order\Grid\Collection for Magento\Sales\Model\ResourceModel\Order\Grid\Collection class in di.xml file as following.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Model\ResourceModel\Order\Grid\Collection" type="Vendor\Module\Model\ResourceModel\Order\Grid\Collection"/>
</config>

Step2. Create Collection.php file inside the app/code/Vendor/Module/Model/ResourceModel/Order/Grid/Collection directory. In this file, we have overrided the _initSelect() method to add filter for our custom_column field.

<?php
namespace Vendor\Module\Model\ResourceModel\Order\Grid;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
use Magento\Sales\Model\ResourceModel\Order;
use Psr\Log\LoggerInterface as Logger;

/**
 * Order grid collection based on tenant
 */
class Collection extends \Magento\Sales\Model\ResourceModel\Order\Grid\Collection
{
    /**
     * @var TimezoneInterface
     */
    private $timeZone;

    /**
     * Initialize dependencies.
     *
     * @param EntityFactory $entityFactory
     * @param Logger $logger
     * @param FetchStrategy $fetchStrategy
     * @param EventManager $eventManager
     * @param string $mainTable
     * @param string $resourceModel
     * @param TimezoneInterface|null $timeZone
     */
    public function __construct(
        EntityFactory $entityFactory,
        Logger $logger,
        FetchStrategy $fetchStrategy,
        EventManager $eventManager,
        $mainTable = 'sales_order_grid',
        $resourceModel = Order::class,
        TimezoneInterface $timeZone = null
    ) {
        parent::__construct(
            $entityFactory,
            $logger,
            $fetchStrategy,
            $eventManager,
            $mainTable,
            $resourceModel,
            $timeZone
        );
        $this->timeZone = $timeZone ?: ObjectManager::getInstance()
            ->get(TimezoneInterface::class);
    }

    /**
     * @inheritdoc
     */
    protected function _initSelect()
    {
        parent::_initSelect();
        
        //filter for custom_column field
        $value = 10;
        $this->addFieldToFilter("custom_column", $value);

        return $this;
    }
}

If you want to display the custom column in the sales order grid. Then refer to the following blog link:
https://webkul.com/blog/add-custom-columns-to-order-grid-in-admin-magento-2/

Hope this will be helpful.
Thanks 🙂

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home