How To Add Discount & Tax Column In Order Grid In Magento 2

Hello Everyone,

In this blog, we will learn about how to Add Tax and Discount Columns in Order Grid in Magento 2.

Order grid is used to display order data like id, purchase date, total, subtotal etc.

However sometimes we require to display some more information about order, one such information is a discount given to the customer. 

Without wasting your time, let us guide you straight away. Follow the easy step given below to Add Column like Discount and Tax in Sales Order Grid in Magento 2.

STEPS FOR ADD DISCOUNT AND TAX COLUMN IN ORDER GRID IN MAGENTO 2

Step 1: Create Collection.php file

app/code/Vendor/Extension/Model/ResourceModel/Order/Grid/Collection.php

<?php

namespace Vendor\Extension\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();

    }

}

Step 2: Create di.xml file

app/code/Vendor/Extension/etc/di.xml

<?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\Extension\Model\ResourceModel\Order\Grid\Collection</item>

            </argument>

        </arguments>

    </type>

    <type name="Vendor\Extension\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>

Step 3: Create sales_order_grid.xml file

app/code/Vendor/Extension/view/adminhtml/ui_component/sales_order_grid.xml

<?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>

Step 4: Finally run the below commands

$ php bin/magento cache:clean

$ php bin/magento cache:flush

Step 5: Output:

Final Thoughts:

So this was the easiest way which we have told you in this blog. This is how you can Add Discount and Tax Column in Sales Order Grid in Magento 2. Hope you liked the blog.

So quickly go to the comment box and tell me how you like this blog?

Stay tuned with us on our site to get new updates of Magento.

Thanks for reading and visiting our site.

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 2

No votes so far! Be the first to rate this post.

Leave a Reply

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