I have created a custom report,
Used below code in my ui_component listing file.
<column name="custom_tax_code" class="Vendor\Module\Ui\Component\Listing\Column\Quantity">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">false</item>
<item name="label" xsi:type="string" translate="true">Tax Code</item>
</item>
</argument>
</column>
<column name="shipping_amount_custom" class="Vendor\Module\Ui\Component\Listing\Column\Quantity" >
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">false</item>
<item name="label" xsi:type="string" translate="true">Shipping Amount</item>
</item>
</argument>
</column>
Then in Quantity.php
class Quantity extends \Magento\Ui\Component\Listing\Columns\Column {
public function prepareDataSource(array $dataSource) {
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as & $item) {
$item['custom_tax_code'] = $taxcode; // used some logic to get this value
$item['shipping_amount_custom'] = $lastItemShippingAmt;// used some logic to get this value
}
}
return $dataSource;
}
}
This code is working fine and it is showing properly in admin Grid, But when exported to CSV, values for these 2 columns are not showing. likewise i have many other custom columns. posted only 2 here.
How to resolve this issue? Can anyone help me out please. Thanks
Best Answer
Magento doesn't render the custom fields on export.
https://github.com/magento/magento2/issues/14014
You should add some specific customization for export. I suppose in case dynamic values you can add plugins for these methods:
https://github.com/magento/magento2/blob/06dbbc86572e110ecb60c52e8f54a8a144262d56/app/code/Magento/Ui/Model/Export/ConvertToCsv.php#L64-L103
https://github.com/magento/magento2/blob/06dbbc86572e110ecb60c52e8f54a8a144262d56/app/code/Magento/Ui/Model/Export/MetadataProvider.php#L154-L170
P.S. Otherwise you can try to add your needed data to collection directly, using custom attributes or join to needed table(s), so that the code
$document->getCustomAttribute($column)->getValue()
retrieve the correct value.Here we can see how was solved the similar issue with empty fields in the exported CSV for customer grid -
\Magento\Customer\Ui\Component\DataProvider\Document::getCustomAttribute()