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.
Opens WPIDE on
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
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.