Magento 2 Export Orders CSV Programmatically

Exporting orders from a Magento 2 store in CSV format can be done programmatically using the Magento 2 API. Here are the steps to export orders CSV programmatically in Magento 2:

  1. Create a custom module: First, create a custom module in Magento 2. You can create a new module using the module creator tool or manually create the module files.
  2. Create a controller file: Next, create a controller file in your custom module. This controller will handle the export process.
  3. Retrieve orders data: In the controller file, use the Magento 2 API to retrieve the orders data that you want to export. You can use the Order Repository interface to retrieve the order data.
  4. Write data to CSV file: Once you have the orders data, you can write it to a CSV file using PHP’s fputcsv() function. This function takes an array of data and writes it to a CSV file in the correct format.
  5. Download the CSV file: After writing the orders data to the CSV file, you can then download the file to the user’s computer using PHP’s header() function.

Here is some sample code to get you started:

php

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\Response\Http\FileFactory;
use Magento\Sales\Api\OrderRepositoryInterface;
class ExportOrders extends \Magento\Framework\App\Action\Action
{ protected $fileFactory; protected $orderRepository;public function __construct(
\Magento\Framework\App\Action\Context $context,
FileFactory $fileFactory,
OrderRepositoryInterface $orderRepository
) {
$this->fileFactory = $fileFactory;
$this->orderRepository = $orderRepository;
parent::__construct($context);
} public function execute()
{ $orders = $this->orderRepository->getList(); $csvData = array(); $csvData[] = array('Order ID', 'Customer Email', 'Order Total');
foreach ($orders as $order) {
$csvData[] = array(
$order->getId(),
$order->getCustomerEmail(),
$order->getGrandTotal()
);}
$fileName = 'orders.csv';
$filePath = 'export/' . $fileName;
$directory = $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
$stream = $directory->openFile($filePath, 'w+');
foreach ($csvData as $rowData) {
$stream->writeCsv($rowData);
} $stream->close();
$content = [
'type' => 'filename',
'value' => $filePath,
'rm' => true
];
$response = $this->fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
return $response;
}
}

This code exports the orders data to a CSV file and downloads it to the user’s computer. You can modify the code to customize the orders data and CSV file format as per your requirement.



  1. Create a custom module: To create a custom module in Magento 2, you need to create a new directory in the app/code directory with the namespace and module name of your choice. For example, if you want to create a module called MyCompany_OrderExport, you can create the directory app/code/MyCompany/OrderExport.
  2. Create a controller file: Within your custom module directory, create a new directory called Controller and create a new PHP file inside it called Export.php. This file will contain the code to handle the order export process.
  3. Retrieve orders data: In the execute() method of your controller file, use the Order Repository interface to retrieve the orders data that you want to export. For example, you can use the getList() method of the Order Repository to retrieve a list of all orders in the store. You can also use filters to retrieve specific orders based on criteria such as order status or date.
  4. Write data to CSV file: Once you have retrieved the orders data, you can write it to a CSV file using PHP’s fputcsv() function. This function takes an array of data and writes it to a CSV file in the correct format. In the example code I provided, the orders data is written to a CSV file using a foreach loop that iterates over each order and adds its data to the CSV file.
  5. Download the CSV file: After writing the orders data to the CSV file, you can then download the file to the user’s computer using PHP’s header() function. In the example code I provided, the CSV file is downloaded using Magento’s FileFactory class, which creates a new file download response that includes the CSV file data.

Once you have created the controller file and written the code to export orders to CSV, you can access the export functionality by visiting the URL of your controller action in your Magento 2 store. For example, if your module is called MyCompany_OrderExport and your controller file is called Export.php, you can access the export functionality by visiting the URL http://yourstore.com/orderexport/export.

Note that the example code I provided is a simplified example that exports only basic order data to a CSV file. Depending on your requirements, you may need to modify the code to export additional data or customize the CSV file format. Additionally, you may need to add error handling and other functionality to ensure that the export process is robust and reliable.