How to Add a Custom Column in Order Grid in Magento 2
The default Magento 2 sales order grid offers a number of columns to easily manage the order processing. However, with the advancing E-commerce options and the majority of preference for online shopping, more data is to be handled, for which, the default order grid may not be enough.
The smooth order process is essential when it comes to customer experience. If the store can handle the order delivery fast and accurate, the customers are prompted to repeat their choice for the next purchase too!
However, it is possible only when the admin can systematically handle the order data. All the data accessible in one place can make it easier. For that, the admin may need to add a custom column in order grid in Magento 2 store which is not offered by default.
The programmatic method to add a custom column in the sales order grid in Magento 2 store is given in the post. You can also add action column in Magento 2 admin grid as the action column enables you to perform record-specific actions such as deleting or editing.
However, you can also opt for easy solution, the Magento 2 Custom Order Grid extension that allows adding 50+ custom columns in the order grid for the easy and accurate order processing system.
Method to add a custom column in order grid in Magento 2 :
- Create di.xml file at app\code\Vendor\Module\etc\ with following code:
12345678910111213141516<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"><type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory"><arguments><argument name="collections" xsi:type="array"><item name="sales_order_grid_data_source" xsi:type="string">Vendor\Module\Model\ResourceModel\Order\Grid\Collection</item></argument></arguments></type><type name="Vendor\Module\Model\ResourceModel\Order\Grid\Collection"><arguments><argument name="mainTable" xsi:type="string">sales_order_grid</argument><argument name="resourceModel" xsi:type="string">Magento\Sales\Model\ResourceModel\Order</argument></arguments></type></config> - Create Collection.php file at app\code\Vendor\Module\Model\ResourceModel\Order\Grid\ with the following code:
123456789101112131415161718192021222324252627282930313233343536<?phpnamespace Vendor\Module\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\Grid\Collection as OriginalCollection;use Psr\Log\LoggerInterface as Logger;/*** Order grid extended collection*/class Collection extends OriginalCollection{protected $helper;public function __construct(EntityFactory $entityFactory,Logger $logger,FetchStrategy $fetchStrategy,EventManager $eventManager,$mainTable = 'sales_order_grid',$resourceModel = \Magento\Sales\Model\ResourceModel\Order::class){parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);}protected function _renderFiltersBefore(){$joinTable = $this->getTable('sales_order');$this->getSelect()->joinLeft($joinTable, 'main_table.entity_id = sales_order.entity_id', ['tax_amount', 'discount_amount', 'coupon_code']);parent::_renderFiltersBefore();}} - Create sales_order_grid.xml file app\code\Vendor\Module\view\adminhtml\ui_component\ with following code:
1234567891011121314151617181920212223242526272829<?xml version="1.0" encoding="UTF-8"?><listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"><columns name="sales_order_columns"><column name="tax_amount" class="Magento\Sales\Ui\Component\Listing\Column\PurchasedPrice"><argument name="data" xsi:type="array"><item name="config" xsi:type="array"><item name="filter" xsi:type="string">textRange</item><item name="label" xsi:type="string" translate="true">Tax Amount</item></item></argument></column><column name="discount_amount" class="Magento\Sales\Ui\Component\Listing\Column\PurchasedPrice"><argument name="data" xsi:type="array"><item name="config" xsi:type="array"><item name="filter" xsi:type="string">textRange</item><item name="label" xsi:type="string" translate="true">Discount Amount</item></item></argument></column><column name="coupon_code"><argument name="data" xsi:type="array"><item name="config" xsi:type="array"><item name="filter" xsi:type="string">text</item><item name="label" xsi:type="string" translate="true">Coupon Code</item></item></argument></column></columns></listing>
That’s it. Also you can include custom column in export to csv in Magento 2 it will helpful to you when you want csv with custom made changes.
Feel free to mention your doubts in the Comments section below, if any, and I’d be happy to help.
You can also add custom column to display product image in order create page and make order creation process seamless!
Please share the above solution with fellow developers on social media. You can also learn to create order in admin panel Magento 2 to assist your customers while placing orders when they require.
Thanks.
A perfect tool for easy and fast order management with custom added columns in the order grid
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
2 Comments
How to add the final shipment date filter in order grid. Could you please suggest for this one?
Hello,
Your issue needs customization for which you can avail professional help from Meetanshi’s certified developers.
Thank you.