Magento: Automatically create CSV File for each order placed

Magento: Automatically create CSV File for each order placed

September 23, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: Automatically create CSV File for each order placed

Automatically Generate CSV Files for Magento Orders

Magento is a powerful e-commerce platform that offers extensive capabilities for managing orders and customer data. If you’re looking to automatically generate CSV files for each order placed in your Magento store, this guide will walk you through the process using PHP and cron jobs.

Why Automate Order CSV Generation?

Automating the creation of CSV files for your orders provides several benefits:

  1. Efficient Dispatch Management: You can quickly access all necessary information to fulfill and dispatch orders without manually compiling data.
  2. Backup and Archive: Having separate CSV files for each order allows you to easily backup or archive important customer and product data.
  3. Integration with Third-Party Systems: Automatically generated CSV files enable seamless integration with external systems, such as inventory management software or ERP systems.

How to Generate Order CSV Files in Magento

To achieve this, we’ll create a custom script that retrieves the required order information using Magento’s built-in APIs and exports it to a CSV format. We’ll then set up a cron job to run the script automatically whenever an order is placed.

Step 1: Create the CSV Generation Script

Create a new PHP file in your Magento root directory, e.g., generate_order_csv.php. Add the following code:

<?php

require_once 'app/Mage.php';
Mage::init();

// Get the last placed order ID
$lastOrderId = Mage::getModel('sales/order')->getCollection()->getLastItem()->getEntityId();

// Retrieve the order details
$order = Mage::getModel('sales/order')->load($lastOrderId);

// Initialize the CSV file name
$csvFileName = 'order_' . $order->getIncrementId() . '_' . date('YmdHis') . '.csv';

// Open the CSV file for writing
$fileHandle = fopen($csvFileName, 'w');

// Write headers to the CSV file
fputcsv($fileHandle, array_keys($order->getAllVisibleItems()));

// Write order details and items to the CSV file
foreach ($order->getAllVisibleItems() as $item) {
    fputcss($filehandle, array_values($item->getData()));
}

// Close the CSV file
fclose($fileHandle);

echo "CSV file generated successfully: {$csvFileName}\n";

Step 2: Set Up the Cron Job

To run the script automatically whenever an order is placed, we’ll create a cron job in your Magento root directory. Create a new text file called cron.php and add the following code:

<?php
require_once 'app/Mage.php';
Mage::init();

// Run the CSV generation script
shell_exec('php generate_order_csv.php');

Step 3: Configure the Cron Job

To configure the cron job, you need to create a new configuration file in your Magento root directory called crontab.xml. Add the following content inside the <config> tag:

<cron>
    <job>
        <schedule>0 1 * * *</schedule>
        <code><![CDATA[shell_exec('php cron.php');]]></code>
    </job>
</cron>

In this example, the script will run every hour at 1 minute past the hour. You can adjust the schedule as needed.

Conclusion

Automatically generating CSV files for your Magento orders is an efficient way to streamline your order management process and integration with external systems. By creating a custom PHP script and setting up a cron job, you can ensure that each newly placed order is automatically exported to a CSV file containing all necessary details.

Don’t forget to test the setup thoroughly and make sure to secure any sensitive data before sharing or integrating these CSV files with third-party systems.

To learn more about Magento development best practices and customization options, visit Laravel Company for comprehensive guides and expert consultations.

Catalog Security API Themes

Our network