Docs Advanced Shipment Tracking Get tracking info from orders

Get tracking info from orders

Use ast_get_tracking_items() to read the tracking information AST has stored on an order. It’s the function to reach for when you want to put the tracking number into an SMS, add it to a custom email or template, or export it to a CSV.

Function

ast_get_tracking_items( int $order_id ) : array

Returns an array of tracking items for the order — one entry per tracking number. An order with no tracking returns an empty array, so always check before you use the result.

Fields in each tracking item

KeyTypeDescription
tracking_idstringThe unique ID of this tracking entry. Use it if you need to identify or delete one specific shipment.
tracking_numberstringThe tracking number.
tracking_providerstringThe carrier slug as stored.
formatted_tracking_providerstringThe carrier’s display name — this is the one to show to a customer.
formatted_tracking_linkstringThe complete tracking URL, with the tracking number already substituted in.
date_shippedintegerThe shipped date as a Unix timestamp. Format it with date_i18n() before displaying it.

Example

This snippet reads the tracking on an order and builds a line of text for each shipment — the shape you’d use for an SMS body or a custom email.

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

	$order_id       = 123; // Replace with your order ID.
	$tracking_items = ast_get_tracking_items( $order_id );

	if ( ! empty( $tracking_items ) ) {

		$lines = array();

		foreach ( $tracking_items as $tracking_item ) {

			$tracking_number   = $tracking_item['tracking_number'];
			$tracking_provider = $tracking_item['formatted_tracking_provider'];
			$tracking_url      = $tracking_item['formatted_tracking_link'];
			$date_shipped      = date_i18n( get_option( 'date_format' ), $tracking_item['date_shipped'] );

			$lines[] = sprintf(
				'Shipped %1$s with %2$s. Tracking %3$s: %4$s',
				$date_shipped,
				$tracking_provider,
				$tracking_number,
				$tracking_url
			);
		}

		$message = implode( "\n", $lines );

		// Use $message — send it by SMS, add it to an email, write it to a CSV.
	}
}

An order can hold more than one tracking number, so always loop rather than reading the first entry only.

See also: Adding tracking info to orders and the shipment tracking REST API.