1

In sales_order table we have a column coupon_code. I need to show it in sales order grid.

I have added the column coupon code by overriding sales_order_grid.xml file. The column is being shown. Now I need to show the values of sales_order.coupon_code.

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="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="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>  

Module/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">
<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>
  • Given answer try it, and it will add the data when the next time it post data in sales_order and that will copy into sales_order_grid as well. – Prathap Gunasekaran Mar 5 at 12:31
  • sure I am trying your solution – Sufyan Khot Mar 5 at 12:34
  • 1
    the coupon code column is still empty – Sufyan Khot Mar 6 at 5:21
  • When the next time data is been posted to sales_order then it will get posted in sales_order_grid table as well. Getting it – Prathap Gunasekaran Mar 6 at 5:21
  • yes I placed a new order and used a coupon code but its not showing in grid. I have updated my question – Sufyan Khot Mar 6 at 5:23
2

EDIT:

Add in or add an InstallSchema.php from this path of your custom module:

Vendor/Module/Setup/InstallSchema.php

<?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();
}
}

Then after, you have to map the coupon_code column to sales_order column in your di.xml configuration:

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

Then in your 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="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="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>   

Note: make sure to run commands after performing changes above:
setup:upgrade
setup:static-content:deploy -f

  • @Sufyan, have you tried the above solution? – magefms Mar 6 at 5:37
  • Do I need to add coupon_code column in sales_order_grid table also? – Sufyan Khot Mar 6 at 6:23
  • no need , just try the code above and it should work – magefms Mar 6 at 6:24
  • The coupon code column is empty in grid. I have updated my question also. – Sufyan Khot Mar 6 at 6:24
  • using code above? have you tried to put data on coupon_code column in your schema? – magefms Mar 6 at 6:25
1

Try this,

Add di.xml in

app/code/vendor/module/etc/di.xml

add the below code in it

<?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">
        <item name="salesperson" xsi:type="string">sales_order.coupon_code</item>
</argument>
</arguments>                                                    
</virtualType>
</config>

Hope this helps :)

  • this solution works only if sales_order_grid already has coupon_code column. You can update your answer. – Sufyan Khot Mar 6 at 7:39
  • That's what I was saying you need to have column, see the question is to show data in grid. Anyway you got it, finally after the code is been given – Prathap Gunasekaran Mar 6 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.