How to Add Coupon Code Column in Magento 2 Order Grid

Order grid helps to add, track, and manage order details for quick and accurate order management. Fast, accurate, and flexible order processing is an inevitable part of any Magento 2 store.

The sales order grid in Magento 2 can be accessed in the admin panel under the “Sales” -> “Orders”. The default order grid offers order-related options like order ID, purchase date, grand total, order status, etc.

However, the default order grid is not enough when the admin wants to add a coupon code column! The requirement of columns in the order grid depends on the business.

Also read: How to Auto Apply Coupon Code in Magento 2

The admin may want to monitor or get the details of the order placed using coupon code for marketing purposes. In that case, you may need to fix Magento 2 reset button in grid column to add coupon code column in Magento 2 order grid to provide a better workflow to store admins.

Use the below solution in order to do so.

Steps to Add Coupon Code Column in Magento 2 Order Grid

1. Create registration.php file at app/code/Vendor/Module and use the below code.

<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

2. Create module.xml at app/code/Vendor/Module/etc and use the below code.

<?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="Vendor_Module" setup_version="1.0.0">
    </module>
</config>

3. Use the below code in the di.xml file at app/code/Vendor/Module/etc

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="Magento\Sales\Model\ResourceModel\Order\Grid" type="Magento\Sales\Model\ResourceModel\Grid">
        <arguments>
            <argument name="columns" xsi:type="array">
                <item name="coupon_code" xsi:type="string">sales_order.coupon_code</item>
            </argument>
        </arguments>
    </virtualType>
</config>

4. Create InstallSchema.php file at app/code/Vendor/Module/Setup

<?php

namespace Vendor\Module\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class InstallSchema implements InstallSchemaInterface
{

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;

        $installer->startSetup();

        /* While module install, creates column in sales_order_grid table */

        $eavTable = $installer->getTable('sales_order_grid');

        $columns = [
            'coupon_code' => [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'nullable' => true,
                'comment' => 'Coupon Code',
            ],
        ];

        $connection = $installer->getConnection();
        foreach ($columns as $name => $definition) {
            $connection->addColumn($eavTable, $name, $definition);
        }

        $installer->endSetup();
    }
}

5. Create sales_order_grid.xml at app/code/Vendor/Module/view/adminhtml/ui_component

<?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="coupon_code">
            <argument name="data" xsi:type="array">
                <item name="js_config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
                </item>
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="align" xsi:type="string">left</item>
                    <item name="label" xsi:type="string" translate="true">Coupon Code</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

6. Execute the below commands.

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush

7. Execute the following SQL query

update sales_order_grid set coupon_code=(select coupon_code from sales_order where sales_order.entity_id=sales_order_grid.entity_id)

Add the column of coupon code and make better convenience in order grid in Magento 2. You can also add action column in admin grid in Magento 2 that enables you to perform record specific actions such as deleting or editing.

The output of the above code displays like the below image:

how to add coupon code column in order grid in Magento 2

Also, you can add any custom column in the order grid in Magento 2 based on your business requirements.

Feel free to share the solution with Magento Community via social media.

Thank You.

Sanjay Jethva

Article by

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