-
-
Save bekarice/f1ba5b8e2f3c62d82a5cbc100dc3e1ad to your computer and use it in GitHub Desktop.
| <?php // only copy if needed | |
| /** | |
| * Example: Add order meta to the REST API | |
| * WC 2.6+ | |
| * | |
| * @param \WP_REST_Response $response The response object. | |
| * @param \WP_Post $post Post object. | |
| * @param \WP_REST_Request $request Request object. | |
| * @return object updated response object | |
| */ | |
| function wc_add_rest_order_meta( $response, $post, $request ) { | |
| $order_data = $response->get_data(); | |
| foreach ( $order_data['line_items'] as $key => $item ) { | |
| $order_data['line_items'][ $key ]['metakey'] = wc_get_order_item_meta( $item['id'], '_meta_value_key', true ); | |
| } | |
| $response->data = $order_data; | |
| return $response; | |
| } | |
| add_filter( 'woocommerce_rest_prepare_shop_order', 'wc_add_rest_order_meta', 10, 3 ); |
Is there anything that would stop this being used to add free-form data to the api?
I've tried it and it seems to not modify output at all. I added a field to $data which is assigned to $response->get_data();.
I later set $response->data = $data; before returning $response so I'm a little uncertain what is wrong.
I have added this to my functions.php but the api output still hasnt changed. The item meta_key I want to add is "shipped".
Same issue here I added
$order_data['line_items'][ $key ]['_wccf_pf_stuecklnge'] = wc_get_order_item_meta( $item['id'], '_wccf_pf_stuecklnge', true );
but it wont appear
Hi everyone, there's a couple changes you'll need to make to get this working with the latest version of the API:
In my code I used get_post_meta for the item, you can change that to whatever you like.
add_filter( 'woocommerce_rest_prepare_shop_order_object', 'my_wc_prepare_shop_order', 10, 3 );
function my_wc_prepare_shop_order( $response, $object, $request ) {
$order_data = $response->get_data();
foreach ( $order_data['line_items'] as $key => $item ) {
$order_data['line_items'][ $key ]['mykey'] = get_post_meta( $item['product_id'], 'mykeyname', true );
}
$response->data = $order_data;
return $response;
}
Thanks a lot, spared me a lot of headache I've been having last couple of days. 😄
Hi everyone, there's a couple changes you'll need to make to get this working with the latest version of the API:
In my code I used
get_post_metafor the item, you can change that to whatever you like.
add_filter( 'woocommerce_rest_prepare_shop_order_object', 'my_wc_prepare_shop_order', 10, 3 );
function my_wc_prepare_shop_order( $response, $object, $request ) {
$order_data = $response->get_data();
foreach ( $order_data['line_items'] as $key => $item ) {
$order_data['line_items'][ $key ]['mykey'] = get_post_meta( $item['product_id'], 'mykeyname', true );
}
$response->data = $order_data;
return $response;
}
this worked for me. thanks
I'm trying to add that code into my function.php file but I can't get the product attribute values from the order detail API to come out, can you please help me.
Thanks for this! Saved me a lot of time, digging through the API class. 👍