How to Overried Sales Order Grid Collection in Magento 2

If you're seeking for an easy solution to override sales order grid collection, you've come to the correct place. In this article, we'll show you how to override the sales order grid in Magento 2.

Let's get started!!

Step 1: Create a di.xml file at app/code/Devhooks/HelloWorld/etc/adminhtml/ and add paste the below code.

<?xml version="1.0"?> <config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Sales\Model\ResourceModel\Order\Grid\Collection" type="Devhooks\HelloWorld\Model\ResourceModel\Order\Grid\Collection"/> </config>

You need to replace the Devhooks and HelloWorld with your [VendorName] and [ModuleName].

Step 2: Create a Collection.php file at app/code/Devhooks/HelloWorld/Model/ResourceModel/Order/Grid/ and add paste the below code.

<?php namespace Devhooks\HelloWorld\Model\ResourceModel\Order\Grid; 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\Sales\Model\ResourceModel\Order; use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as OriginalCollection; use Psr\Log\LoggerInterface as Logger; /** * Order grid extended collection */ class Collection extends OriginalCollection { /** * Collection constructor. * @param EntityFactory $entityFactory * @param Logger $logger * @param FetchStrategy $fetchStrategy * @param EventManager $eventManager * @param string $mainTable * @param string $resourceModel */ public function __construct( EntityFactory $entityFactory, Logger $logger, FetchStrategy $fetchStrategy, EventManager $eventManager, $mainTable = 'sales_order_grid', $resourceModel = Order::class ) { parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel); } protected function _renderFiltersBefore() { //Add you login here start. $this->getSelect()->where("attribute_code = filter_value"); //Add you login here end. parent::_renderFiltersBefore(); } }

In above code, you need to replace the attribute_code and filter filter_value and per your requirements.

Step 3: Now, open Command line in folder root of magento and run both commands.

php bin/magento s:up; php bin/magento s:s:d -f; php bin/magento setup:di:compile; php bin/magento c:c;

That's it.

You have successfully overridden the sales order grid, we hope this article helped you to know How to Overried Sales Order Grid Collection in Magento 2.

Bookmark it for your future reference. Do comment below if you have any other questions or doubts.

P.S. Do share this post with your team.

Review other articles maybe it'll help you too.

Recent Articles
Chrome Extension
Copyright © 2024 All rights reserved.
Ads OFF toggle_off