Skip to content

Instantly share code, notes, and snippets.

@sarathlal-old
Last active August 13, 2021 14:55
Show Gist options
  • Save sarathlal-old/a131dae9a0ad8340744804d414237020 to your computer and use it in GitHub Desktop.
Save sarathlal-old/a131dae9a0ad8340744804d414237020 to your computer and use it in GitHub Desktop.
/*
* For latest WooCommerce Rest API
* Add a referanse field to the Order API response.
*/
function prefix_wc_rest_prepare_order_object( $response, $object, $request ) {
// Get the value
$referanse_meta_field = ( $value = get_post_meta($object->get_id(), '_billing_referanse', true) ) ? $value : '';
$response->data['referanse'] = $referanse_meta_field;
return $response;
}
add_filter( 'woocommerce_rest_prepare_shop_order_object', 'prefix_wc_rest_prepare_order_object', 10, 3 );
/*
* For Legacy API
* Add a referanse field to the Order API response.
*/
function prefix_wc_api_order_response( $order_data ) {
// Get the value
$referanse_meta_field = ( $value = get_post_meta($order_data['id'], '_billing_referanse', true) ) ? $value : '';
$order_data['referanse'] = $referanse_meta_field;
return $order_data;
}
add_filter( 'woocommerce_api_order_response', 'prefix_wc_api_order_response', 10, 1 );
/*
For customer response
*/
add_filter( 'woocommerce_rest_prepare_customer', 'th35tr_override_rest_response_customer', 10, 3 );
function th35tr_override_rest_response_customer($response, $user_data, $request){
$meta_value_1 = ( $value = get_user_meta( $user_data->ID, 'field_name_1' , true ) ) ? $value : '';
$response->data['field_name_1'] = $meta_value_1;
return $response;
}
/*
For subscription response
*/
function th25er_modify_wc_rest_subscription_object( $response, $object, $request ) {
// Get order ID
$order_id = $response->data['parent_id'];
// Get the value
$value = ( $value = get_post_meta($order_id, 'discord_username', true) ) ? $value : '';
$response->data['billing']['discord_username'] = $value;
return $response;
}
add_filter( 'woocommerce_rest_prepare_shop_subscription', 'th25er_modify_wc_rest_subscription_object', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment