cancel
Showing results for 
Search instead for 
Did you mean: 

How to Add a New Column to Orders Grid in Magento 2.2.2

How to Add a New Column to Orders Grid in Magento 2.2.2

Hello everyones, how to add a New Column to Orders Grid?.

i need add a columns in the grid and add in the table sales_order_grid,  attribute see in Order View to modify.

http://docs.magento.com/m2/ce/user_guide/sales/order-processing.html

Admin: Sales > Orders

_________________________________________________________________

ID            |   PurchasePoint |  PurchaseData  |  ...  |   ...   |  ...  | ...  | MyAttributeNew

_________________________________________________________________

000001      Main Website     Mar 23, 2018   ...         ...            ...       ...    My note

 

 

 

in Order View, how to modify my information?

5 REPLIES

Re: How to Add a New Column to Orders Grid in Magento 2.2.2

QaisarSatti/HelloWorld/view/adminhtml/ui_component/sales_order_grid.xml

 

<!-- Add the column "my_column" (My Column) to the sales order grid. -->
<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="my_column">
        <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">My Column</item>
            </item>
        </argument>
    </column>
</columns>

QaisarSatti/HelloWorld/etc/di.xml

 

it your column name in sales_order_grid table

<!-- Sync the column "my_column" between the sales_order and sales_order_grid tables -->
<virtualType name="Magento\Sales\Model\ResourceModel\Order\Grid" type="Magento\Sales\Model\ResourceModel\Grid">
    <arguments>
        <argument name="columns">
            <item name="my_column" xsi:type="string">sales_order.my_column</item>
        </argument>
    </arguments>
</virtualType>

 

 

Resource

Find helpful ? Consider Giving Kudos to this post.
Problem solved? Click Accept as Solution!"
Qaisar Satti

Re: How to Add a New Column to Orders Grid in Magento 2.2.2

Thank you friend @qaisar_satti i like you response, but i resolved this problem.

1.png

now a problem new.

How to add information in the column new?.

in Sales>order Order View i need modify the column in orders correctly.

2.png

 

 

 

Re: How to Add a New Column to Orders Grid in Magento 2.2.2

hi @arturo_cabrera

 

Here is available solution for that

Find helpful ? Consider Giving Kudos to this post.
Problem solved? Click Accept as Solution!"
Qaisar Satti

Re: How to Add a New Column to Orders Grid in Magento 2.2.2

No, that solution is for add a new element to the table in orders.

i need add information for my column new

Re: How to Add a New Column to Orders Grid in Magento 2.2.2

You can follow below step:

1. You will have to create an attribute or order entity in magento(its quite easy). Which will be further added in your sales_order database table and you will need to add a column in your sales_order_grid table as well, because Magento backend order grid is loaded from this table. Ideally in your install schema you will just need below code.(I am taking Depot as an example)

        $setup->startSetup();
        $setup->getConnection()->addColumn(
            $setup->getTable('sales_order_grid'),
            'depot',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                'comment' => 'Depot',
                'length' => 255
            ]
        );
        $setup->endSetup();
        

2. Create an observer for either "sales_order_save_before" event or "checkout_onepage_controller_success_action" based upon your requirement and in your observer just create a setter for your entity(Depot for example)

Observer for "sales_order_save_before"

public function execute(\Magento\Framework\Event\Observer $observer)
{
$order = $observer->getEvent()->getOrder();
$order->setDepot($nearestDepot);
return $this; }

Observer for "checkout_onepage_controller_success_action"

 

 

public function execute(\Magento\Framework\Event\Observer $observer)
{
        $orderId = $observer->getEvent()->getOrderIds();
        $order = $this->order->load($orderId);
        $order->setDepot("Some Depot");
$order->save();
return $this; }

Now above code is just rough but note that if your event is triggered before order place you have to use the setter only and if your event is triggered after order is placed, you will have to fetch the order and set your data and save that order.

 

3. Now with above working fine your column will be created in both the needed tables(Magento will auto add your data in sales_order_grid). You can go to admin backend and just above the grid you have an option to select which columns you want in your table and you can select your column(depot).

Hope that will help.