Last active
November 4, 2019 17:28
-
-
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)
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 | |
/* | |
* 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