Skip to content

Instantly share code, notes, and snippets.

@TimBHowe
Last active August 2, 2017 05:29
Show Gist options
  • Save TimBHowe/d9b42e2cea5bf49d5207c8007001a129 to your computer and use it in GitHub Desktop.
Save TimBHowe/d9b42e2cea5bf49d5207c8007001a129 to your computer and use it in GitHub Desktop.
WooCommerce - Add custom post_meta data to the order REST API response.
<?php
/**
* Hook into order API response to add custom field data.
*
* @param $order_data array
* @param $order WC_Order
* @param $fields array
* @param $server array
*
* @return $order_data array
*/
add_filter( 'woocommerce_api_order_response', 'add_foo_field_order_api_response', 20, 4 );
function add_foo_field_order_api_response( $order_data, $order, $fields, $server ) {
// Get the custom order post_meta data and add it to the order_data array.
$order_data['foo'] = get_post_meta( $order->id, 'foo', true );
return $order_data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment