How to Include Discount Column in Admin Order Grid without Adding Field in sales_order_grid table in Magento 2
Order grid in Magento 2 displays the order data. The order grid lists the order data like order ID, purchase date, order amount, customer name who placed the order, grand total, subtotal, payment method, etc.
The order data in grid format makes it easy for the admin to process every order, manage the delivery, and smoothly fulfil each order.
The default sales order grid in the backend facilitates the option to show the selected columns among the default options.
However, owing to modern business requirements, the admin may require to add a custom column in order grid in Magento 2 for better order processing.
Faster and smoother order processing gives a delightful shopping experience. It also reduces the admin’s workload.
One such instance is to add the custom discount column in admin order grid. The admin can easily manage the discounts assigned with each order, analyze the benefits of offering discounts, etc.
The programmatic method to add discount column in admin order grid without adding field in sales_order_grid table in Magento 2 is easy, as shown below:
Method to Include Discount Column in Admin Order Grid Without Adding Field in sales_order_grid Table in Magento 2:
- Create app/code/Meetanshi/DiscountColumn/Model/ResourceModel/Order/Grid/Collection.php
123456789101112131415161718192021222324252627282930313233343536<?phpnamespace Meetanshi\DiscountColumn\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']);parent::_renderFiltersBefore();}} - Create /app/code/Meetanshi/DiscountColumn/etc/di.xml
1234567891011121314151617<?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">Meetanshi\DiscountColumn\Model\ResourceModel\Order\Grid\Collection</item></argument></arguments></type><type name="Meetanshi\DiscountColumn\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 /app/code/Meetanshi/DiscountColumn/etc/module.xml
12345<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"><module name="Meetanshi_DiscountColumn" setup_version="1.0.0"/></config> - Create /app/code/Meetanshi/DiscountColumn/view/adminhtml/ui_component/sales_order_grid.xml
123456789101112131415161718192021<?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</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</item></item></argument></column></columns></listing> - Create app/code/Meetanshi/DiscountColumn/registration.php/
1234<?phpuse \Magento\Framework\Component\ComponentRegistrar;ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Meetanshi_DiscountColumn', __DIR__);
You can download the extension from our Github or implement the steps as below:
That’s it.
Once done, you can see the discount column in order grid:
Any doubts about the solution can be mentioned in the Comments section below. I’ll be happy to help.
Also, do share the post with the Magento community via social media.
Thank you.
Magento 2 Coupon Code Link extension makes it easy to apply coupon code using a link instead of manually typing the code.
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
I am getting Notice: Undefined index: tax_amount in /var/www/staging/app/code/Magento/Sales/Ui/Component/Listing/Column/PurchasedPrice.php on line 68
error on magento 2 order grid list.
Can you please let me know how to fix this issue?
Hello,
We have implemented the above solution and there’s no error. Please check again.
Thank You.