Created
December 28, 2016 20:18
-
-
Save bekarice/f1ba5b8e2f3c62d82a5cbc100dc3e1ad to your computer and use it in GitHub Desktop.
Example: Add order item meta to WC REST API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this worked for me. thanks