From 1d52eef313c7f004d7aaee25959a64f26b0200ad Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Mon, 2 May 2022 10:50:30 -0700 Subject: [PATCH] REST: Add product image URL to order line items (#32710) Add a new property to the line item schema, `image` that contains `id` and `src` properties for the full size of the main product image. Fixes #27364 --- .../fix-27364-api-order-lineitem-image | 4 +++ .../class-wc-rest-orders-v2-controller.php | 34 +++++++++++++++++-- .../rest-api/Tests/Version2/orders.php | 6 +++- .../rest-api/Tests/Version3/orders.php | 6 +++- 4 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 plugins/woocommerce/changelog/fix-27364-api-order-lineitem-image diff --git a/plugins/woocommerce/changelog/fix-27364-api-order-lineitem-image b/plugins/woocommerce/changelog/fix-27364-api-order-lineitem-image new file mode 100644 index 00000000000..2a5334b4527 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-27364-api-order-lineitem-image @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Add an 'image_src' field to product line items returned by the orders endpoint (v2 and v3). This contains the URL of the main product image. diff --git a/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-orders-v2-controller.php b/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-orders-v2-controller.php index 8bda913c60a..4acd85a2e85 100644 --- a/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-orders-v2-controller.php +++ b/plugins/woocommerce/includes/rest-api/Controllers/Version2/class-wc-rest-orders-v2-controller.php @@ -214,10 +214,16 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller { } } - // Add SKU and PRICE to products. + // Add SKU, PRICE, and IMAGE to products. if ( is_callable( array( $item, 'get_product' ) ) ) { - $data['sku'] = $item->get_product() ? $item->get_product()->get_sku() : null; - $data['price'] = $item->get_quantity() ? $item->get_total() / $item->get_quantity() : 0; + $data['sku'] = $item->get_product() ? $item->get_product()->get_sku() : null; + $data['price'] = $item->get_quantity() ? $item->get_total() / $item->get_quantity() : 0; + + $image_id = $item->get_product() ? $item->get_product()->get_image_id() : 0; + $data['image'] = array( + 'id' => $image_id, + 'src' => $image_id ? wp_get_attachment_image_url( $image_id, 'full' ) : '', + ); } // Add parent_name if the product is a variation. @@ -1487,6 +1493,28 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller { 'context' => array( 'view', 'edit' ), 'readonly' => true, ), + 'image' => array( + 'description' => __( 'Properties of the main product image.', 'woocommerce' ), + 'type' => 'object', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + 'properties' => array( + 'type' => 'object', + 'properties' => array( + 'id' => array( + 'description' => __( 'Image ID.', 'woocommerce' ), + 'type' => 'integer', + 'context' => array( 'view', 'edit' ), + ), + 'src' => array( + 'description' => __( 'Image URL.', 'woocommerce' ), + 'type' => 'string', + 'format' => 'uri', + 'context' => array( 'view', 'edit' ), + ), + ), + ), + ), ), ), ), diff --git a/plugins/woocommerce/tests/legacy/unit-tests/rest-api/Tests/Version2/orders.php b/plugins/woocommerce/tests/legacy/unit-tests/rest-api/Tests/Version2/orders.php index ead32b074f4..0983cd63414 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/rest-api/Tests/Version2/orders.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/rest-api/Tests/Version2/orders.php @@ -545,6 +545,10 @@ class WC_Tests_API_Orders_V2 extends WC_REST_Unit_Test_Case { 'sku' => null, 'price' => 4, 'parent_name' => null, + 'image' => array( + 'id' => 0, + 'src' => '', + ), ); $this->assertEquals( 200, $response->get_status() ); @@ -785,7 +789,7 @@ class WC_Tests_API_Orders_V2 extends WC_REST_Unit_Test_Case { $data = $response->get_data(); $line_item_properties = $data['schema']['properties']['line_items']['items']['properties']; - $this->assertEquals( 15, count( $line_item_properties ) ); + $this->assertEquals( 16, count( $line_item_properties ) ); $this->assertArrayHasKey( 'id', $line_item_properties ); $this->assertArrayHasKey( 'meta_data', $line_item_properties ); $this->assertArrayHasKey( 'parent_name', $line_item_properties ); diff --git a/plugins/woocommerce/tests/legacy/unit-tests/rest-api/Tests/Version3/orders.php b/plugins/woocommerce/tests/legacy/unit-tests/rest-api/Tests/Version3/orders.php index 794c313757f..3211cd2a26d 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/rest-api/Tests/Version3/orders.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/rest-api/Tests/Version3/orders.php @@ -657,6 +657,10 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case { 'sku' => null, 'price' => 4, 'parent_name' => null, + 'image' => array( + 'id' => 0, + 'src' => '', + ), ); $this->assertEquals( 200, $response->get_status() ); @@ -1155,7 +1159,7 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case { $data = $response->get_data(); $line_item_properties = $data['schema']['properties']['line_items']['items']['properties']; - $this->assertEquals( 15, count( $line_item_properties ) ); + $this->assertEquals( 16, count( $line_item_properties ) ); $this->assertArrayHasKey( 'id', $line_item_properties ); $this->assertArrayHasKey( 'meta_data', $line_item_properties ); $this->assertArrayHasKey( 'parent_name', $line_item_properties );