Magento 2: Add Column to Admin Order Grid

Magento 2: Add Column to Admin Order Grid - SbDevBlog

Magento 2 provides a solid UI grid to view the data. We can see order columns in the orders grid under the sales tab. There are several visible columns in the grid. Also, there are several hidden columns. The visibility can be managed by the columns option given in the toolbar of the UI grid.

We are often required to display custom columns in the order grid. Adding a sales order grid is pretty straightforward. So, Let’s check out how we can add a custom field to the sales order grid.

We can add a new column by using the UI Component XML. The sales_order_grid.xml is used as a UI component for the sales order grid. The sales_order_columns tag is responsible for handling the columns in sales_order_columns.xml.

Secondly, we must define and pass a filter to the data provider’s collection factory by providing a custom field using a join query. Then, we must pass the table name of sales_order_grid and the resource model of the order table to that collection.

Magento 2: Add Column to Admin Order Grid

Create a file with the name sales_order_grid.xml inside the ui_component directory of the admihtml area inside the view directory of the custom module.

<?xml version="1.0" encoding="UTF-8"?>

<listing xmlns:xsi=""
    <columns name="sales_order_columns">
        <column name="sbdevblog_comment">
                <label translate="true">SBDEVBLOG Comment</label>

Create a di.xml file inside the etc directory of the custom module.

<?xml version="1.0"?>
<config xmlns:xsi=""

    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
            <argument name="collections" xsi:type="array">
                <item name="sales_order_grid_data_source" xsi:type="string">SbDevBlog\Checkout\Model\ResourceModel\Order\Grid\Collection</item>
    <type name="SbDevBlog\Checkout\Model\ResourceModel\Order\Grid\Collection">
            <argument name="mainTable" xsi:type="string">sales_order_grid</argument>
            <argument name="resourceModel" xsi:type="string">Magento\Sales\Model\ResourceModel\Order</argument>

Let’s pass a field using join. We must create a class defined in the di.xml file to do that.

 * @copyright Copyright (c) sbdevblog (
namespace SbDevBlog\Checkout\Model\ResourceModel\Order\Grid;

class Collection extends \Magento\Sales\Model\ResourceModel\Order\Grid\Collection

     * Filtering sbdevblog comment
     * @return void
    protected function _renderFiltersBefore()
        $joinTable = $this->getTable('sales_order');
            'main_table.entity_id = sales_order.entity_id',

That’s it. Please create a field in the sales_order table before implementing the above solution. So, let’s create a db_schema.xml file to create a field.

<?xml version="1.0"?>
<schema xmlns:xsi=""
    <table name="sales_order" resource="default" engine="innodb">
        <column name="sbdevblog_comment" nullable="true" xsi:type="varchar" length="50" comment="SbDevBlog Comment"/>

Just replace sbdevblog_comment with your custom field, and you are ready.

Thanks for reading the post, Magento 2: Add Column to Admin Order Grid. Please use the comment section for your feedback. Please share and subscribe to SBDEVBLOG.

Click to add validation before placing the order.

Download Source Code

Note: Please verify the code of this blog and the relevant git repository before using it in production.
sb dev blog adobe commece Magento 2


Leave a Reply

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