2
votes

I am trying to create a new value to line_items in the Orders endpoint, but it shows an error that I don't know how to solve!

The code is from: Woocommerce REST API extending order response

Other helpful links:

The code

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 );

The Error:

Your PHP code changes were rolled back due to an error on line 336 of file wp-content/themes/store-child/functions.php. Please fix and try saving again.

Uncaught Error: Using $this when not in object context in wp-content/themes/store-child/functions.php:336
Stack trace:
#0 wp-settings.php(528): include()
#1 wp-config.php(89): require_once('/kunden/631823_...')
#2 wp-load.php(37): require_once('/kunden/631823_...')
#3 wp-admin/admin.php(34): require_once('/kunden/631823_...')
#4 wp-admin/theme-editor.php(10

Thanks!!

1

1 Answers

2
votes

If the function needs to be added to your theme's functions.php and not inside a class, try replacing:

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

with:

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

It should resolve the error you posted.