Magento shipment tracking

Magento shipment tracking

September 23, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento shipment tracking

Magento Shipment Tracking: Exporting Order Data to CSV

Hello! I’m glad you’re exploring Magento’s order management features, especially shipment tracking. Let’s dive into how you can export the required data from your Magento database in CSV format.

Understanding Magento’s Order Structure

Magento stores order data across multiple tables in its MySQL database. The primary table is sales_flat_order which contains basic order information like order ID, status, and customer details.

For shipment tracking numbers, Magento stores them in the sales_shipment_track table. Each row in this table represents a single tracking number associated with an order.

Exporting Required Order Data to CSV

To export your desired data, you can use PHP’s built-in functions for reading MySQL data and writing it to files. Here’s a step-by-step guide:

  1. First, create a new PHP file (e.g., export_orders.php) in your Magento root directory.

  2. Include the necessary database connection settings at the top of the script:

<?php
$hostname = 'your_hostname';
$dbname   = 'your_db_name';
$username = 'your_username';
$password = 'your_password';

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}

Replace the placeholders with your actual database credentials.

  1. Write a SQL query to fetch the required order data:
$sql = "
SELECT
    o.entity_id AS 'Order Number',
    a.street AS 'Shipping Address',
    a.city AS 'Shipping City',
    a.region AS 'Shipping State',
    a.postcode AS 'Shipping Zip',
    c.phone_number AS 'Phone Number',
    st.tracking_number AS 'Tracking Number'
FROM
    sales_flat_order o
JOIN
    sales_flat_address a ON o.shipping_address_id = a.entity_id
JOIN
    customer_entity c ON o.customer_id = c.entity_id
LEFT JOIN
    sales_shipment_track st ON o.entity_id = st.order_id
";

This query joins the necessary tables to retrieve all required fields for each order. The LEFT JOIN is used here because some orders might not have any tracking numbers.

  1. Execute the SQL query and fetch results into an array:
$stmt = $dbh->query($sql);
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
  1. Open a file pointer to write data in CSV format:
$file = fopen('export_orders.csv', 'w');
if ($file === FALSE) {
    die("Could not open output file");
}
  1. Write the array data into the CSV file using fputcsv() function:
foreach ($data as $row) {
    fputcsv($file, $row);
}
  1. Close the file pointer:
fclose($file);
echo "Export complete! Check 'export_orders.csv' in your Magento root directory.";

Additional Best Practices

  • Remember to backup your database before running any export scripts.
  • Always test your queries on a staging or development environment first.
  • If you’re working with large datasets, consider using MySQL’s SELECT INTO OUTFILE statement instead of PHP for better performance.

That’s it! You should now have a CSV file named export_orders.csv in your Magento root directory containing the specified order data, including shipment tracking numbers.

For more advanced customization and automation of this process, I recommend checking out our guide on Magento Data Export Best Practices.

Happy exporting! Let me know if you have any other questions.

Database Configuration Orders Customers

Our network