I added a QR Code column in my admin grid, where I concatenate my order_id and increment_id.
class QrCode extends Column
{
public function prepareDataSource(array $dataSource)
{
if(isset($dataSource['data']['items'])) {
foreach($dataSource['data']['items'] as &$items) {
$increment_id = $items['increment_id'];
$order_id = $items['order_id'];
$items['qrcode'] = $increment_id." ".$order_id;
}
}
return $dataSource;
}
}
It worked and displayed in my admin grid perfectly but it won't show when I export it to CSV. What should I do?
-
Hope this will help you. magento.stackexchange.com/questions/184040/…Nits– Nits2019年11月20日 06:28:25 +00:00Commented Nov 20, 2019 at 6:28
1 Answer 1
In order to add your custom column QrCode to the export csv, You need to override and extend the class Magento\Ui\Model\Export\ConvertToCsv and rewrite the method getCsvFile to include your custom field QrCode
source
https://github.com/magento/magento2/blob/2.3.3/app/code/Magento/Ui/Model/Export/ConvertToCsv.php
Sample Code (not tested, you must correct this based on your needs)
Your Module (DI) file
app/code/StackExchange/Override/etc/adminhtml/di.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Ui\Model\Export\ConvertToCsv" type="StackExchange\Override\Model\Export\ConvertToCsv" />
</config>
Overriden Class ConvertToCsv.php
app/code/StackExchange/Override/Model/Export/ConvertToCsv.php
<?php
/*
* Rewritten Export Model Convert To CSV class to include the custom field added to the grid
*
*
* @author Jerome Dennis <[email protected]>
*
*/
namespace StackExchange\Override\Model\Export\;
class ConvertToCsv extends \Magento\Ui\Model\Export\ConvertToCsv
{
/**
* Returns CSV file
*
* @return array
* @throws LocalizedException
*/
public function getCsvFile()
{
$component = $this->filter->getComponent();
$name = md5(microtime());
$file = 'export/'. $component->getName() . $name . '.csv';
// Here goes your code to include your custom field QrCode
return [
'type' => 'filename',
'value' => $file,
'rm' => true // can delete file after use
];
}
}
UPDATE
As alternate solutions, you can also use the solution provided here or this one