Halloween Sale 15% off Ends in See the plans
PHP

Add Invoice, Tracking and Delivery Data to Order API Responses

Includes invoice numbers, tracking details, delivery dates and other order fields you list in WooCommerce REST API order responses, for accounting and shipping integrations.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_rest_order_meta = array( // Response field => order meta key.
    'invoice_number' => '_wpide_invoice_number',
    'carrier'        => '_wpide_carrier',
    'tracking'       => '_wpide_tracking',
    'delivery_date'  => '_wpide_delivery_date',
    'po_number'      => '_wpide_po_number',
);

add_filter( 'woocommerce_rest_prepare_shop_order_object', function ( $response, $order ) use ( $wpide_rest_order_meta ) {
    $extra = array();

    foreach ( $wpide_rest_order_meta as $field => $key ) {
        $extra[ $field ] = $order->get_meta( $key );
    }

    $response->data['wpide'] = $extra;

    return $response;
}, 10, 2 );

Notes

Read-only; the default keys match other WPIDE snippets (invoice numbers, tracking, delivery date) and you can add your own.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.