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
This commit is contained in:
Corey McKrill 2022-05-02 10:50:30 -07:00 committed by GitHub
parent 322e833eb8
commit 1d52eef313
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 5 deletions

View File

@ -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.

View File

@ -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' ),
),
),
),
),
),
),
),

View File

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

View File

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