3

I'm trying to override the Export button of the Admin Orders Grid, to add another custom CSV export with fixed columns

The goal for the custom option is to always export the same columns, without changing the default behavior of CSV and Excel XML filtered columns. Something to do with the dataProvider I guess

Code:

../view/adminhtml/ui_component/sales_order_grid.xml

<?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">
 <exportButton class="Magento\Ui\Component\ExportButton" name="export_button">
 <argument name="data" xsi:type="array">
 <item name="config" xsi:type="array">
 <item name="selectProvider" xsi:type="string">sales_order_grid.sales_order_grid.sales_order_columns.ids</item>
 <item name="options" xsi:type="array">
 <item name="cvs_test" xsi:type="array">
 <item name="value" xsi:type="string">csv_custom</item>
 <item name="label" xsi:type="string" translate="true">CSV_CUSTOM</item>
 <item name="url" xsi:type="string">mui/export/gridToCsv</item>
 </item>
 </item>
 </item>
 </argument>
 </exportButton>
 </listingToolbar>
</listing>

added this also

Modify the sales_order_grid layout file (optional): If you want to override the sales_order_grid layout file to include your custom UI component, create the view/adminhtml/layout/sales_order_grid.xml

 <?xml version="1.0" encoding="UTF-8"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
 <body>
 <referenceBlock name="adminhtml.sales.order.grid.additional">
 <block class="Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface"
 name="custom_export_button">
 <arguments>
 <argument name="data" xsi:type="array">
 <item name="config" xsi:type="array">
 <item name="options" xsi:type="array">
 <item name="csv_custom" xsi:type="array">
 <item name="visible" xsi:type="boolean">true</item>
 <item name="label" xsi:type="string"
Mohamed Hakkim
4371 gold badge3 silver badges12 bronze badges
asked Sep 25, 2018 at 11:08

2 Answers 2

2

After some searching, solved by following this solution and adapted to the CSV format: Magento2: Export grid data in xls format. Not sure if it is the right way but it just works.

For reference, this is code that I modified to have fixed exported columns:

Namespace\Module\Model\Export\MetadataProviderCustomCsv.php

[...]
protected function getColumns(UiComponentInterface $component)
 {
 [...]
 // Define the columns to export
 $baseColumn = reset($this->columns[$component->getName()]);
 $columns = ["entity_id"]; // put here your columns to export
 $columnsComponent = [];
 foreach ($columns as $columnKey) {
 $column = clone($baseColumn);
 $column->setData("name", $columnKey);
 $columnsComponent[$columnKey] = $column;
 }
 $this->columns[$component->getName()] = $columnsComponent;
 return $this->columns[$component->getName()];
 }
 /**
 * Retrieve Headers row array for Export
 *
 * @param UiComponentInterface $component
 * @return string[]
 */
 public function getHeaders(UiComponentInterface $component)
 {
 return ['Entity ID']; // Labels
 }
[...]

Pros: flexible export, it is also possible to easily join other entities, like the order product

Cons: it requires at least one column selected in the filter but I'm sure there is a way to create a column without cloning it

answered Sep 26, 2018 at 9:51
1

You will have to override uicomponent file sale_order_grid.xml in your module and create corresponding admin controller

<?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">
 <exportButton name="export_button" class="Magento\Ui\Component\ExportButton">
 <argument name="data" xsi:type="array">
 <item name="config" xsi:type="array">
 <item name="options" xsi:type="array">
 <item name="csv" xsi:type="array">
 <item name="value" xsi:type="string">csv</item>
 <item name="label" xsi:type="string" translate="true">CSV</item>
 <item name="url" xsi:type="string">mui/export/gridToCsv</item>
 </item>
 <item name="xml" xsi:type="array">
 <item name="value" xsi:type="string">xml</item>
 <item name="label" xsi:type="string" translate="true">Excel XML</item>
 <item name="url" xsi:type="string">mui/export/gridToXml</item>
 </item>
 <item name="customcsv" xsi:type="array">
 <item name="value" xsi:type="string">customcsv</item>
 <item name="label" xsi:type="string" translate="true">Custom CSV</item>
 <item name="url" xsi:type="string">routefrontname/export/gridToCustomcsv</item>
 </item>
 </item>
 </item>
 </argument>
 </exportButton>
</listingToolbar>

`

for complete code check my module

answered May 25, 2023 at 15:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.