Docs Advanced Shipment Tracking Pro (AST PRO) Retrieving Tracking Information from Orders

Retrieving Tracking Information from Orders

The ast_get_tracking_items() function returns every tracking entry recorded on an order, with the carrier name, tracking link and carrier icon already resolved. Use it to read tracking data programmatically — for example, to pass tracking info to an SMS or email plugin, show it in a custom template, export it to a CSV, or send it to a third-party system.

The function is defined in includes/tracking-info.php and works with both the classic posts table and HPOS, because it reads the order through wc_get_order().

Function reference

ast_get_tracking_items( int $order_id ): array
ParameterTypeDescription
$order_idintThe WooCommerce order ID to retrieve tracking for.
Return valuearrayA numerically indexed array of tracking entries, one per shipment, in the order they were added. Returns an empty array if the order does not exist or has no tracking.

Tracking entry fields

Each entry is an associative array. The first group is what AST PRO stores on the order; the second group is added at read time by the formatter, so it always reflects your current carrier settings.

Stored fields

KeyTypeDescription
tracking_idstringUnique ID of this entry (an MD5 hash). Use it to identify or delete a specific shipment.
tracking_numberstringThe raw tracking number.
tracking_providerstringThe carrier slug, for example usps or fedex. Not the display name — use formatted_tracking_provider for that.
custom_tracking_linkstringA tracking URL supplied with the entry, if any. Used as the fallback link when the carrier is not recognised.
tracking_product_codestringSecondary code some carriers need to build the tracking URL. Usually empty.
date_shippedintUnix timestamp of the shipped date.
status_shippedint|stringWhat was requested when the entry was added: 0 no status change, 1 mark as Shipped, 2 mark as Partially Shipped. Empty string if not set.
sourcestringWhere the entry came from, for example orders, edit_order, REST_API, bulk_paste or integration. Empty for older entries.
products_listarray|stringPer-item tracking data. An array of objects when Tracking Per Item was used; an empty string otherwise. Each object has product (product ID), item_id (order item ID, may be empty) and qty (quantity in this shipment).

Formatted fields (added at read time)

KeyTypeDescription
formatted_tracking_providerstringThe carrier display name, for example USPS. Empty if the slug matches no enabled carrier.
formatted_tracking_linkstringThe full tracking URL, built from the carrier’s URL pattern (or your custom URL for that carrier) with the tracking number and, where the carrier needs it, the shipping postcode and country. Falls back to custom_tracking_link.
ast_tracking_linkstringThe link after the ast_tracking_link filter has run. This is what AST PRO itself prints in emails and on the order page, so prefer it over formatted_tracking_link when you want to match what the customer sees (for example, a TrackShip tracking page).
tracking_provider_imagestringURL of the carrier icon.

Code snippet

<?php
if ( function_exists( 'ast_get_tracking_items' ) ) {

    $order_id = 123; // Replace with your order ID

    $tracking_items = ast_get_tracking_items( $order_id );

    foreach ( $tracking_items as $tracking_item ) {

        $tracking_id       = $tracking_item['tracking_id'];
        $tracking_number   = $tracking_item['tracking_number'];
        $provider_slug     = $tracking_item['tracking_provider'];           // e.g. usps
        $tracking_provider = $tracking_item['formatted_tracking_provider']; // e.g. USPS
        $tracking_url      = $tracking_item['ast_tracking_link'];           // what the customer sees
        $carrier_icon      = $tracking_item['tracking_provider_image'];
        $date_shipped      = date_i18n( get_option( 'date_format' ), $tracking_item['date_shipped'] );

        // Per-item tracking (only an array when Tracking Per Item was used)
        if ( ! empty( $tracking_item['products_list'] ) && is_array( $tracking_item['products_list'] ) ) {
            foreach ( $tracking_item['products_list'] as $item ) {
                $product_id = $item->product; // WooCommerce product ID
                $qty        = $item->qty;     // Quantity in this shipment
                $product    = wc_get_product( $product_id );
                if ( $product ) {
                    $product_name = $product->get_name();
                }
            }
        }
    }
}

Example: add tracking to a WooCommerce email

A common use is printing every shipment, with a link, inside an order email. Add this to your theme’s functions.php or a code-snippets plugin:

<?php
add_action( 'woocommerce_email_order_details', 'my_email_tracking_list', 5, 4 );

function my_email_tracking_list( $order, $sent_to_admin, $plain_text, $email ) {

    if ( ! function_exists( 'ast_get_tracking_items' ) ) {
        return;
    }

    $tracking_items = ast_get_tracking_items( $order->get_id() );

    if ( empty( $tracking_items ) ) {
        return;
    }

    foreach ( $tracking_items as $tracking_item ) {

        $provider = $tracking_item['formatted_tracking_provider'];
        $number   = $tracking_item['tracking_number'];
        $link     = $tracking_item['ast_tracking_link'];
        $shipped  = date_i18n( get_option( 'date_format' ), $tracking_item['date_shipped'] );

        if ( $plain_text ) {
            echo "{$provider}: {$number} ({$shipped}) {$link}\n";
        } else {
            printf(
                '<p>%s: <a href="%s">%s</a> - %s</p>',
                esc_html( $provider ),
                esc_url( $link ),
                esc_html( $number ),
                esc_html( $shipped )
            );
        }
    }
}

Filtering the result

Before the list is returned it passes through the ast_get_tracking_items filter. Anything you change there is also picked up by AST PRO’s own emails and templates, and by third-party email builders that read tracking through the same function.

<?php
/**
 * @param array $tracking_items The tracking entries.
 * @param int   $order_id       The order ID.
 * @param bool  $formatted      Whether the formatted fields were added (true for ast_get_tracking_items()).
 */
add_filter( 'ast_get_tracking_items', function ( $tracking_items, $order_id, $formatted ) {
    // Example: hide entries that have no tracking number
    return array_values( array_filter( $tracking_items, function ( $item ) {
        return '' !== $item['tracking_number'];
    } ) );
}, 10, 3 );

Notes

  • Always wrap the call in function_exists( 'ast_get_tracking_items' ) so the code fails gracefully when AST PRO is not active.
  • tracking_provider is the carrier slug. For the name customers see, read formatted_tracking_provider; for the link customers see, read ast_tracking_link.
  • products_list is an array only when Tracking Per Item is enabled in General Settings and the entry was added with SKU/quantity data. For standard tracking it is an empty string, so check is_array() before looping.
  • Read $item->qty from the raw product object in products_list — not from the WC_Product returned by wc_get_product(), which has no qty property.
  • The formatted fields are built on every call from your current carrier settings. If you rename a carrier or change its tracking URL, existing entries pick up the change the next time they are read.
  • The same data is also available from the REST API at GET /wp-json/wc-ast/v3/orders/{id}/shipment-trackings — see Shipment Tracking API.

Related: Adding Tracking Info to Orders