1

Thanks! for view this post.

I just want to know if there is any feasibility in which we can add extra columns in grid using extension attributes?

Please share your steps with me.

Thanks!

1

It is possible to use extension attributes bei creating a Plugin for the grid collection. (see paypal/braintree module, which uses also an extension-attribute-table for the sales grid)

etc/adminthml/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">
    <!-- Sale order grid -->
    <type name="Magento\Sales\Model\ResourceModel\Order\Grid\Collection">
        <plugin name="DrSalesOrderGrid" type="\Dr\Sales\Plugin\SalesOrderGridPlugin" />
    </type>
</config>

Plugin/SalesOrderGridPlugin:

<?php

namespace Dr\Sales\Plugin;
use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Model\ResourceModel\Order\Grid\Collection;

class SalesOrderGridPlugin
{
    /**
     * @param Collection $subject
     * @return null
     * @throws LocalizedException
     */
    public function beforeLoad(Collection $subject)
    {
        if (!$subject->isLoaded()) {
            $primaryKey = $subject->getResource()->getIdFieldName();
            $tableName = $subject->getResource()->getTable('dr_sales_order_extension');

            $subject->getSelect()->joinLeft(
                $tableName,
                $tableName . '.sales_order_id = main_table.' . $primaryKey,
                $tableName . '.dr_deliverydate'
            );
        }

        return null;
    }
}

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="dr_deliverydate">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">date</item>
                    <item name="label" xsi:type="string" translate="true">Lieferdatum</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>
0

Vendor/Module/etc/db_schema.xml

<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="sales_order">
        <column xsi:type="int"
                name="custom_column_id"
                padding="10"
                unsigned="true"
                nullable="true"
                comment="Custom Column Id"/>
    </table>
</schema>

Thank you

3
  • Hi @Nirav, Thanks for your response. – Rajan Soni May 19 '20 at 10:38
  • Yes, Using extension attributes we can add the custom attribute to the entity. Using the extension attribute I get the custom attribute in order object in API now I want if I add the column in sales order using XML then that field will automatically take the value from my extension attribute. Does It makes sense? – Rajan Soni May 19 '20 at 10:42
  • If your custom attribute is custom column of flat table then you only need to add column in listing ui component xml file. otherwise you need to modify dataprovider also as described in last link I shared in answer. you can check it for more details. – Nirav Patel May 20 '20 at 4:26

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.