Created
May 23, 2020 23:44
-
-
Save yanknudtskov/ccd970657852b4a9c70e8cbdc55ea8af to your computer and use it in GitHub Desktop.
Extend the output of the WooCommerce REST API for order line items
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 | |
| /** | |
| * 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 | |
| */ | |
| add_filter( 'woocommerce_rest_prepare_shop_order_object', 'yanco_wc_rest_api_extend_order_lineitems_data', 10, 3 ); | |
| function yanco_wc_rest_api_extend_order_lineitems_data( $response, $object, $request ) { | |
| if( empty( $response->data ) ) { | |
| return $response; | |
| } | |
| $order_data = $response->get_data(); | |
| foreach ( $order_data['line_items'] as $key => $item ) { | |
| $_product_id = wc_get_order_item_meta( $item['id'], '_product_id', true ); | |
| $_stock_status = get_post_meta( $_product_id, '_stock_status', true ); | |
| $_stock = get_post_meta( $_product_id, '_stock', true ); | |
| $order_data['line_items'][ $key ]['stock_status'] = $_stock_status; | |
| $order_data['line_items'][ $key ]['stock'] = $_stock; | |
| } | |
| $response->data = $order_data; | |
| return $response; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment