How can I get all order tracking numbers in Magento?

How can I get all order tracking numbers in Magento?

July 31, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How can I get all order tracking numbers in Magento?

Retrieving All Order Tracking Numbers in Magento

When working with order management in Magento, you may find yourself needing to retrieve all tracking numbers associated with an order, particularly for shipped packages. While the official documentation provides some guidance, it can be unclear how to correctly fetch this data.

Understanding the Problem: When the Current Approach Fails

You’ve likely tried using the getTrackingNumbers() method on your order object, expecting it to return all tracking numbers. The issue arises because this method relies on the sales_flat_order_item table’s tracking_number column, which is not always populated with data.

The Root Cause: Tracking Numbers Are Stored Differently

In Magento, tracking numbers are stored in a separate table called sales_flat_order_status_history. This table records all changes to an order’s status and includes the tracking number when a shipment is created. However, it doesn’t directly relate to individual line items.

The Solution: A Two-Step Approach

To retrieve all tracking numbers for an order, you need to follow a two-step process:

Step 1: Get All Shipment IDs

First, fetch all shipment IDs associated with the given order using the getShipmentsCollection() method on your order object. This will return a collection of all shipments related to this order.

$shipmentCollection = $order->getShipmentsCollection();

Step 2: Extract Tracking Numbers from Shipment History

Next, iterate over each shipment in the collection and retrieve its tracking numbers by looking at the sales_flat_order_status_history table. The entity_id of the history record is equal to the shipment ID.

$trackingNumbers = [];
foreach ($shipmentCollection as $shipment) {
    $historyCollection = Mage::getResourceModel('sales/order_status_history_collection')
        ->addFieldToFilter('parent_id', $shipment->getId())
        ->addFieldToSelect(array('entity_id'));

    foreach ($historyCollection as $history) {
        if (isset($history->getData()['comment'])) {
            $trackingNumbers[] = $history->getData()['comment'];
        }
    }
}

Best Practices and Troubleshooting

Remember, this approach assumes that you have correctly configured your Magento installation to save tracking numbers in the sales_flat_order_status_history table. If you’re still having issues, ensure:

  1. Your order has been fully processed.
  2. Shipment status is set to “Complete”.
  3. Tracking number was entered during shipment creation.

Conclusion: Retrieving Tracking Numbers Made Easy

By understanding where Magento stores tracking numbers and following the two-step process outlined above, you can efficiently retrieve all tracking numbers for a given order. This approach ensures you have access to the complete data needed for your application’s requirements.

For more detailed information on Magento order management, we recommend exploring our comprehensive guides at Laravel Company.

Database Orders Errors

Our network