I have a column in sales_order table. I want to show this column in all grids in admin side.
I have already shown it in sales_order_gdir. But couldn't find it on sales_invoice grid, sales_shippment_grid and sales_creditmemo_grid.
Please help.
You have to create virtual type for the grid and also need to create join query for the table join.
Please follow guide for more reference :- https://amasty.com/knowledge-base/how-to-add-a-custom-column-to-the-order-grid-in-magento-2.html
Step 1:- Create ResourceModel for your field join in your custom module.
in the _renderFiltersBefore method, you can add the columns that you want, for example, we’ve used the discount_amount column. In this array, you can pass as many parameters from the sales_order table as you like. Also, here you can join the other one or several tables to use the needed parameters for the order.
<?php namespace Vendor\Module\Model\ResourceModel\Order\Grid; use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy; use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory; use Magento\Framework\Event\ManagerInterface as EventManager; use Magento\Sales\Model\ResourceModel\Order; use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as OrderGridCollection; use Psr\Log\LoggerInterface as Logger; class Collection extends OrderGridCollection { public function __construct( EntityFactory $entityFactory, Logger $logger, FetchStrategy $fetchStrategy, EventManager $eventManager, $mainTable = 'sales_order_grid', $resourceModel = Order::class ) { parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel); } protected function _renderFiltersBefore() { $joinTable = $this->getTable('sales_order'); // ADD YOUR TABLE NAME $this->getSelect()->joinLeft( ['sales_order_table' => $joinTable], 'main_table.entity_id = sales_order_table.entity_id', ['discount_amount'] // you can add other attributes here ); parent::_renderFiltersBefore(); } } Step 2:- Create a ui grid file in your view directly.
For invoice grid create below file.
Vendor/Module/view/adminhtml/ui_component/sales_order_invoice_grid.xml
In the ui grid file you have to define your column name and datasource.
For the data source file you can review core Magento class and make your own class accordingly.
vendor/magento/module-sales/Ui/Component/Listing/Column/PurchasedPrice.php
<?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_invoice_columns"> /* Here you have to add columns name as per define in the ui component file */ <column name="discount_amount" class="Magento\Sales\Ui\Component\Listing\Column\PurchasedPrice"> /* Here you can pass your own class (datasource) and you can return data as per your logic */ <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="filter" xsi:type="string">textRange</item> <item name="label" xsi:type="string" translate="true">Discount Amount</item> </item> </argument> </column> </columns> </listing> Step 3:- Create di.xml and define virtual type for it.
Vendor/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"> <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory"> <arguments> <argument name="collections" xsi:type="array"> <item name="sales_order_grid_data_source" xsi:type="string">Vendor\Module\Model\ResourceModel\Order\Grid\Collection</item> /* Add your ResourceModel class here */ </argument> </arguments> </type> <type name="Vendor\Module\Model\ResourceModel\Order\Grid\Collection"> <arguments> <argument name="mainTable" xsi:type="string">sales_invoice</argument> /* Pass here main table name which is responsible for the data return */ <argument name="resourceModel" xsi:type="string">Magento\Sales\Model\ResourceModel\Order\Invoice\Grid</argument>/* Pass here ResourceModel for the particular grid */ </arguments> </type> </config> Save files and clean the cache.
I hope this will help you.