How to get order meta from array in WooCommerce 3+

How to get order meta from array in WooCommerce 3+

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to get order meta from array in WooCommerce 3+

When integrating third-party plugins like WooCommerce Dymo Print, you often find yourself needing to extract specific data points from the complex structure of a WooCommerce Order object. The core issue arises when standard property access fails because the data is nested within an associative array rather than being exposed as a direct getter method.

In the provided scenario, the goal is to return $order_data['delivery_date'] for a shipping label. However, looking at the raw meta structure, the delivery_date isn't at the top level of $order->get_data(). Instead, it sits deep within a nested hierarchy: inside shipment, then options, before finally reaching the actual timestamp string. Attempting to access $order_data['delivery_date'] directly will return null or an empty string because that key simply does not exist at that depth in the returned array structure.

To solve this, you need to traverse the specific path that matches your meta structure. In WooCommerce 3+, the get_data() method returns a raw associative array representing the order's metadata. You must manually index into these arrays to reach the nested value.

Here is the corrected approach:

add_filter('wc_dymo_order_function', 'wpf_dymo_order_output', 10, 3);

function wpf_dymo_order_output( $order_id, $label, $object ) {
    
    // Ensure we only process shipping labels with the DEL_DATE object type
    if ( $label === 'shipping' && $object === 'DEL_DATE' ) {
        
        // Fetch the order instance
        $order = wc_get_order( $order_id );

        // Get the raw order data array
        $order_data = $order->get_data();

        // Navigate to the specific nested key: shipment -> options -> delivery_date
        // We use isset() to prevent PHP notices if the path doesn't exist for some orders
        if ( isset( $order_data['shipment']['options']['delivery_date'] ) ) {
            return $order_data['shipment']['options']['delivery_date'];
        }

        return '';
    }

    return '';
}

The critical change here is replacing the direct key access with a chained navigation pattern: $order_data['shipment']['options']['delivery_date']. This mirrors the exact structure shown in your meta dump. If get_data() does not expose this deep nesting directly, you might need to look at $order->get_meta('delivery_date', true), but based on the provided array structure, direct array indexing is required.

A common pitfall when working with nested WooCommerce data is relying solely on the raw output of get_data(). While convenient, this method exposes the full backend structure which can vary between themes or plugin versions. If you encounter inconsistent results, consider using $order->get_meta() to retrieve custom fields by key. However, for standard meta that is stored as a serialized string within the database and returned as an array in get_data(), direct indexing remains the most reliable method for WooCommerce 3+.

Another consideration is data integrity. Not every order will have a delivery date set. Always check if the nested keys exist before accessing them to avoid PHP warnings or unexpected null returns that might break your label generation logic. Using isset() or the null coalescing operator (??) ensures your plugin handles orders without this specific metadata gracefully.

If you are managing multiple plugins or need to normalize order data across different contexts, creating a helper function to safely traverse these arrays can save significant debugging time later. This pattern of deep nesting is typical in WooCommerce's internal meta handling, where complex logistical data is stored as nested JSON-like structures within the meta table.

Our network