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!
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityIt 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>
Extention attributes allows you to add your custom attribute in api request or response so by using that you can easily add your custom attributes in API.
Regarding create new column in sales_order table you can use declarative schema for that.
Declarative schema allows to create and modify table.
Refer below links for more details.
https://devdocs.magento.com/guides/v2.3/extension-dev-guide/declarative-schema/db-schema.html
Modify or add column in sales_order table.
Create db_schema.xml file under your module's etc folder.
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