Magento 2: Add Custom Sale Order Grid Column and Export to CSV

magento2magento2.2.6

i added custom columns in Sales Order Grid successfully, but when i export data in CSV file the custom column blanked

<?php
namespace Vender\Module\Ui\Component\Listing\Column;

use \Magento\Sales\Api\OrderRepositoryInterface;
use \Magento\Framework\View\Element\UiComponent\ContextInterface;
use \Magento\Framework\View\Element\UiComponentFactory;
use \Magento\Ui\Component\Listing\Columns\Column;
use \Magento\Framework\Api\SearchCriteriaBuilder;

class CGST extends Column
{
    protected $_orderRepository;
    protected $_searchCriteria;

    public function __construct(ContextInterface $context, UiComponentFactory $uiComponentFactory, OrderRepositoryInterface $orderRepository, SearchCriteriaBuilder $criteria, array $components = [], array $data = [])
    {
        $this->_orderRepository = $orderRepository;
        $this->_searchCriteria  = $criteria;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {

                $order  = $this->_orderRepository->get($item["entity_id"]);
                $export_status = $order->getData("cgst");

                $item[$this->getData('name')] = $export_status;
            }
        }

        return $dataSource;
    }
}

and here is my xml file

<?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="cgst" class="Vender\Module\Ui\Component\Listing\Column\CGST">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="visible" xsi:type="string">true</item>
                    <item name="label" xsi:type="string" translate="true">CGST</item>
                </item>
            </argument>
        </column>

    </columns>
</listing>

Best Answer

For export functionality /vendor/magento/module-ui/Model/Export/ is responsible to export data. so you need to look in to this core file and override below two file and customize as per your requirement.

/vendor/magento/module-ui/Model/Export/ConvertToCsv.php

And

/vendor/magento/module-ui/Model/Export/ConvertToXml.php

Then, you need to add custom code on function getCsvFile()

And you need to add SKU column to sales order grid to render data.

For reference Check this link

I hope it helps!

Related Topic