How to add sales order table custom value to magento 2 report from a custom module
-
22-02-2021 - |
質問
I created a custom module to check the ad code (measure the marketing traffic) value from the url and add to the order. I added a column to the sales order table and save the ad value when user place the order.
Now I need to show this value in the sales report.
Can someone help me how to achieve this?
解決
Try this,
Add a column in sales_order_grid table like you have in sales_order
Then add a sales_order_grid.xml in the below path
app/code/Vendor/ModuleName/view/adminhtml/ui_component/sales_order_grid.xml
add the below code in it
<?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="column_name_in_table">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Column Name Goes here</item>
<item name="visible" xsi:type="boolean">false</item>
</item>
</argument>
</column>
</columns>
</listing>
then add di.xml in the below path
app/code/Vendor/ModuleName/etc/di.xml
add the below code in it
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<virtualType name="Magento\Sales\Model\ResourceModel\Order\Grid" type="Magento\Sales\Model\ResourceModel\Grid">
<arguments>
<argument name="columns">
<item name="customer_manager" xsi:type="string">sales_order.column_name_in_table</item>
</argument>
</arguments>
</virtualType>
</config>
column_name_in_table is the name of column which you have in sales_order table and make you have same column in sales_order_grid table as well.
NOTE : When you post data into sales_order table next time then it will also be added into sales_order_grid table as then xml will list the column in grid view and you can add filter and sortable option to it.
Run below commands after you made these changes
php bin/magento setup:upgrade
php bin/magento cache:flush
Hope this helps :)