Skip to content

Instantly share code, notes, and snippets.

@davidchc
Last active November 4, 2019 17:28
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/21b6fce5b403a47511b0a3e417020e07 to your computer and use it in GitHub Desktop.
Save davidchc/21b6fce5b403a47511b0a3e417020e07 to your computer and use it in GitHub Desktop.
Add Product Custom Field in Listing Returning WooCommerce Rest API (Version WC 3.7.1) - Adicionar campo personalizado do produto na listagem retornando API Rest do WooCommerce (Versão WC 3.7.1)
<?php
/*
* Add Product Custom Field in Listing Returning WooCommerce Rest API (Version WC 3.7.1)
* Adicionar campo personalizado do produto na listagem retornando API Rest do WooCommerce (Versão WC 3.7.1)
*/
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