Extract a ShipStation Tracking Number From WooCommerce Order Meta

Quick Summary
- Load the WooCommerce order with wc_get_order(), read its mailchimp_woocommerce_landing_site meta, and pull the ShipStation tracking_number from that URL's query string.
- get_meta() works for orders stored as posts or in HPOS tables, and parse_url() plus parse_str() turn the query string into an array.
- Confirm your meta key and parameter name on a real order, and add isset() or empty() checks, since missing data triggers errors.
This WooCommerce snippet reads a tracking number from an order’s meta data. In the setup it was written for, the ShipStation tracking number is stored as a tracking_number parameter inside a URL, and that URL is saved in the order meta field mailchimp_woocommerce_landing_site. The code pulls the number out of the URL’s query string.
Before you use it, open an order and check where your tracking number is actually stored: the meta key and the parameter name must match your data. Use the code wherever you already have an order ID, such as a hook callback in a custom plugin or your child theme’s functions.php.
$order = wc_get_order( $order_id );
$mailchimp_message = $order->get_meta('mailchimp_woocommerce_landing_site');
$parts = parse_url($mailchimp_message);
parse_str($parts['query'], $query);
$track_number = $query['tracking_number'];How the code reads the tracking number from WooCommerce order meta
wc_get_order()loads the order.$order_idmust already be set, for example by the hook you use the code in.get_meta()reads the meta field. It works whether orders are stored as posts or in WooCommerce’s custom order tables (HPOS), so prefer it overget_post_meta()for orders.parse_url()splits the stored URL into its parts, andparse_str()turns the query string into the$queryarray.$query['tracking_number']is the value you want. Change the key if your parameter has a different name.
The code doesn’t check that anything exists. If the ID isn’t a valid order, wc_get_order() returns false and the next line fails. If the field is empty, or the URL has no query string or no tracking_number, PHP shows warnings and $track_number has no value. Check with isset() or empty() before you use the number, and escape it with esc_html() when you print it.
Related articles
- adding details to WooCommerce order views and emails: hooks that receive the order, where the number could be printed
- adding and removing WooCommerce action callbacks: how the hook callback around this code is registered
