Skip to content

Instantly share code, notes, and snippets.

@davidchc
Created November 4, 2019 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidchc/8d936a4c92b234f96bd09d2c404be34d to your computer and use it in GitHub Desktop.
Save davidchc/8d936a4c92b234f96bd09d2c404be34d to your computer and use it in GitHub Desktop.
Add Product Custom Field in Listing Returning WooCommerce Rest API
<?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