I want to add a column to sales order grid on magento admin dashboard. But the value of the column is from some process, not from database. Is that possible? And how to do it? Thanks in advance.
You can add a column to the admin grid by adding a file called view/adminhtml/ui_component/sales_order_grid.xml to your custom module with the following content:
<?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">
<listingToolbar name="listing_top"/>
<columns name="sales_order_columns">
<column name="order_reference" class="Vendor\Example\Ui\Component\Listing\Column\Example">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">text</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="label" xsi:type="string" translate="true">Example Column</item>
</item>
</argument>
</column>
</columns>
</listing>
Your Example.php-file should extend Magento\Ui\Component\Listing\Columns\Column and have a prepareDataSource()-method to populate the data:
/**
* @param array $dataSource
* @return array
*/
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as & $item) {
$item[$this->getData('name')] = 'Something'
}
}
return $dataSource;
}
Note that if you want to add sort- and filter-options you need to add some other adjustments, but that depends on what kind of data you want to show in the column.