0

I need to change order report export file name "export.csv" to "custom.csv"

3 Answers 3

1

As mentioned previously, the class Magento\Reports\Controller\Adminhtml\Report\Sales\ExportSalesCsv is responsible for order report.

Instead of rewriting the class controller, we can replace its constructor's argument. In your module, create etc/adminhtml/di.xml:

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\Reports\Controller\Adminhtml\Report\Sales\ExportSalesCsv">
        <arguments>
            <argument name="fileFactory" xsi:type="object">Anna\Report\Model\App\Response\Http\FileFactory</argument>
        </arguments>
    </type>

</config>

Then you can create the following file in Model/App/Response/Http/FileFactory.php:

<?php

declare(strict_types=1);

namespace Anna\Report\Model\App\Response\Http;

use Magento\Framework\App\Filesystem\DirectoryList;

class FileFactory extends \Magento\Backend\App\Response\Http\FileFactory
{
    public function create(
        $fileName,
        $content,
        $baseDir = DirectoryList::ROOT,
        $contentType = 'application/octet-stream',
        $contentLength = null
    ) {
        $fileName = 'custom.csv';

       return parent::create($fileName, $content, $baseDir, $contentType, $contentLength);
    }
}
1

The order export report exports the file with name "sales.csv", which is hard coded in the file /vendor/magento/module-reports/Controller/Adminhtml/Report/Sales/ExportSalesCsv.php.

Similarly, for other reports, you can find the name in their respective controller classes.

You can override the controller in your custom module and change the exported file name changing it like:

$fileName = 'sales.csv';

to

$fileName = 'custom.csv';

Hope this helps.

1

To change the order export file name in Magento 2 from "export.csv" to "custom.csv," you would need to override the relevant controller responsible for generating the export file. Specifically, you would need to modify the \Magento\Reports\Controller\Adminhtml\Report\Sales\Order::exportCsv() method.

Here's a basic outline of the steps you would need to take:

Create a custom module or use an existing one in your Magento 2 instance.

Create a di.xml file in your module's etc/adminhtml directory to override the controller. This file should look something like this:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Reports\Controller\Adminhtml\Report\Sales\Order" type="Your\Module\Controller\Adminhtml\Report\Sales\Order" />
</config>

Create a new controller class that extends \Magento\Reports\Controller\Adminhtml\Report\Sales\Order and override the exportCsv() method. In this method, you can modify the file name before it's returned. Here's an example of how you might do this:

<?PHP
namespace Your\Module\Controller\Adminhtml\Report\Sales;

use Magento\Framework\App\Response\Http\FileFactory;

class Order extends \Magento\Reports\Controller\Adminhtml\Report\Sales\Order
{
    /**
     * Export order report grid to CSV format action
     *
     * @return \Magento\Backend\Model\View\Result\Callback
     */
    public function execute()
    {
        $fileName = 'custom.csv'; // Change the file name here
        $grid = $this->_view->getLayout()->createBlock(
            'Magento\Reports\Block\Adminhtml\Sales\Orders\Grid'
        );
        return $this->_fileFactory->create($fileName, $grid->getCsvFile(), \Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR);
    }
}

Flush the cache and test the changes.

Please note that this is a basic example and may need to be adjusted based on your specific requirements and Magento version. Additionally, overriding core controllers is generally not recommended, so proceed with caution and consider the implications for future updates and compatibility.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.