Created
November 4, 2019 17:17
-
-
Save davidchc/8d936a4c92b234f96bd09d2c404be34d to your computer and use it in GitHub Desktop.
Add Product Custom Field in Listing Returning WooCommerce Rest API
This file contains 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 | |
function wc_add_rest_line_items( $response, $post, $request ) { | |
foreach( $response->data['line_items'] as $item_id => $line_item ) { | |
if ( ! empty( $line_item['product_id'] ) ) { | |
$product_id = ! empty( $line_item['variation_id'] ) ? $line_item['variation_id'] : $line_item['product_id']; | |
$meta_key = 'Assigned Field Name'; | |
$meta_key_selected = 'Selected Field Name'; | |
$response->data['line_items'][ $item_id ][$meta_key] = get_post_meta( $product_id, $meta_key_selected, true ); | |
} | |
} | |
return $response; | |
} | |
add_filter( 'woocommerce_rest_prepare_shop_order_object', 'wc_add_rest_line_items', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment