Woocommerce REST API extending order response
Asked Answered
H

2

11

I'm looking for a way to extend the wc-api/vX/orders/ reponse. I've added multiple custom fields to the checkout (for eg: relation number, delivery date etc). These meta are saved within the order (wp_postmeta table). But why are they not returned with the api?

Normally you can extend the api response with some code like:

add_action( 'rest_api_init', 'custom_register_api_fields' );

function custom_register_api_fields() {
    register_rest_field( 'shop_order','relation_number',
    array(
        'get_callback'    => 'custom_api_meta_callback',
        'update_callback' => null,
        'schema'          => null,
    )
    );
}

/**
*
* @param array $object Details of current post.
* @param string $field_name Name of field.
* @param WP_REST_Request $request Current request
*
* @return mixed
*/

function custom_api_meta_callback( $object, $field_name, $request ) {
 return get_post_meta( $object[ 'id' ], $field_name, true );
}

But when I test the response (with Postman and the php lib), my-website.co/wc-api/v2/orders the custom meta are not visible.

Is there a way to register api fields for the wc-api?

Tnx!

Hosea answered 25/11, 2016 at 10:0 Comment(4)
Hi there @bas. I am trying to update custom fields on orders, and I found this post. Did you try it? #36369888Laurin
Yes! That worked for me.Hosea
great, I also got it working by using the method from the answer above, overriding the C# wrapper Order class as described on the wrapper's Github in the comments. :)Laurin
The answer is here!! #66988196Megilp
S
6

i have the same requirement, add new value to "line_items" in order response

am using wc api v2

https://website.com/wp-json/wc/v2/orders

function get_product_order_image( $response, $object, $request ) {
 
    if( empty( $response->data ) )
        return $response;
    $order_pid= $response->data['line_items'][0]['product_id'];
     $l_w_product_meta = get_post_meta($response->data['line_items'][0]['product_id']);
    $order_imgUrl= wp_get_attachment_url( $l_w_product_meta['_thumbnail_id'][0], 'full' );

    $response->data['line_items'][0]['cover_image'] = $order_imgUrl;
 
    return $response;
} 

add_filter( "woocommerce_rest_prepare_shop_order_object", array( $this, "get_product_order_image"), 10, 3 );

Result cover image added to line item result

i hope this will help someone in future.

Stets answered 11/12, 2018 at 14:25 Comment(3)
Hey @Shameem I've searched all over for how to end the woocommerce api and you the first I've seen! I would love to receive your help, please let me know if you are willing to assist. Many thanks!Jacobinism
@Jacobinism yes please!Stets
Thanks for your reply @Shameem I've created a question here #54387726 , lets continue our conversation there! Thank you! I also maybe away for a bit but will be back soon!Jacobinism
N
2

REST API hook for add new value (product image) to "line_items" in order response for simple product and variable product both

Also Use for multiple products

function get_product_order_image( $response, $object, $request ) {

    if( empty( $response->data ) )
        return $response;
        
    $images = array();
    
    foreach($response->data['line_items'] as $key => $productItems){
        $productID = $productItems['product_id'];
        $variationID = $productItems['variation_id'];
        
        if($variationID == 0){
            $thumbnailID = get_post_meta( $productID, '_thumbnail_id', true);
            $attachment = wp_get_attachment_image_src($thumbnailID, 'woocommerce_thumbnail' );
            $image = $attachment[0];
        }else{
            $variation = new WC_Product_Variation( $variationID );
            $image_id = $variation->get_image_id();
            $attachment = wp_get_attachment_image_src($image_id, 'woocommerce_thumbnail' );
            $image = $attachment[0];
        }
        
        $response->data['line_items'][$key]['image'] = $image;
    
    }     

    return $response;
} 

add_filter( "woocommerce_rest_prepare_shop_order_object", "get_product_order_image", 10, 3 );

Request:

wp-json/wc/v3/orders

wp-json/wc/v3/orders/XXX

wp-json/wc/v3/orders/?customers=XXX

Nottingham answered 2/3, 2021 at 14:2 Comment(1)
Thank you...I was looking for thisMeasles

© 2022 - 2024 — McMap. All rights reserved.