From 0df2a0ec3ffa4ffc7a4f37cee5328f9f2ca0d5aa Mon Sep 17 00:00:00 2001 From: Thomas Roberts <5656702+opr@users.noreply.github.com> Date: Tue, 5 Sep 2023 13:52:11 +0100 Subject: [PATCH] Update cart API response and data store to contain product type (#10825) --- assets/js/previews/cart.ts | 2 ++ assets/js/types/type-defs/cart.ts | 1 + src/StoreApi/Schemas/V1/CartItemSchema.php | 1 + src/StoreApi/Schemas/V1/ItemSchema.php | 6 ++++++ src/StoreApi/docs/cart-items.md | 14 ++++++++------ tests/php/StoreApi/Routes/CartItems.php | 3 +++ 6 files changed, 21 insertions(+), 6 deletions(-) diff --git a/assets/js/previews/cart.ts b/assets/js/previews/cart.ts index d5dceee95ef..27de1187cfb 100644 --- a/assets/js/previews/cart.ts +++ b/assets/js/previews/cart.ts @@ -39,6 +39,7 @@ export const previewCart: CartResponse = { { key: '1', id: 1, + type: 'simple', quantity: 2, catalog_visibility: 'visible', name: __( 'Beanie', 'woo-gutenberg-products-block' ), @@ -120,6 +121,7 @@ export const previewCart: CartResponse = { { key: '2', id: 2, + type: 'simple', quantity: 1, catalog_visibility: 'visible', name: __( 'Cap', 'woo-gutenberg-products-block' ), diff --git a/assets/js/types/type-defs/cart.ts b/assets/js/types/type-defs/cart.ts index d0ecf0095b8..8ee9703cae3 100644 --- a/assets/js/types/type-defs/cart.ts +++ b/assets/js/types/type-defs/cart.ts @@ -123,6 +123,7 @@ export type CatalogVisibility = 'catalog' | 'hidden' | 'search' | 'visible'; export interface CartItem { key: string; id: number; + type: string; quantity: number; catalog_visibility: CatalogVisibility; quantity_limits: { diff --git a/src/StoreApi/Schemas/V1/CartItemSchema.php b/src/StoreApi/Schemas/V1/CartItemSchema.php index c2d9a4fe63d..859f4599ee6 100644 --- a/src/StoreApi/Schemas/V1/CartItemSchema.php +++ b/src/StoreApi/Schemas/V1/CartItemSchema.php @@ -50,6 +50,7 @@ public function get_item_response( $cart_item ) { return [ 'key' => $cart_item['key'], 'id' => $product->get_id(), + 'type' => $product->get_type(), 'quantity' => wc_stock_amount( $cart_item['quantity'] ), 'quantity_limits' => (object) ( new QuantityLimits() )->get_cart_item_quantity_limits( $cart_item ), 'name' => $this->prepare_html_response( $product->get_title() ), diff --git a/src/StoreApi/Schemas/V1/ItemSchema.php b/src/StoreApi/Schemas/V1/ItemSchema.php index 7c86e8e5d3d..20e8b5ca024 100644 --- a/src/StoreApi/Schemas/V1/ItemSchema.php +++ b/src/StoreApi/Schemas/V1/ItemSchema.php @@ -19,6 +19,12 @@ public function get_properties() { 'context' => [ 'view', 'edit' ], 'readonly' => true, ], + 'type' => [ + 'description' => __( 'The item type.', 'woo-gutenberg-products-block' ), + 'type' => 'string', + 'context' => [ 'view', 'edit' ], + 'readonly' => true, + ], 'id' => [ 'description' => __( 'The item product or variation ID.', 'woo-gutenberg-products-block' ), 'type' => 'integer', diff --git a/src/StoreApi/docs/cart-items.md b/src/StoreApi/docs/cart-items.md index ce0fc4fd070..8f122ad7087 100644 --- a/src/StoreApi/docs/cart-items.md +++ b/src/StoreApi/docs/cart-items.md @@ -2,12 +2,12 @@ ## Table of Contents -- [List Cart Items](#list-cart-items) -- [Single Cart Item](#single-cart-item) -- [Add Cart Item](#add-cart-item) -- [Edit Single Cart Item](#edit-single-cart-item) -- [Delete Single Cart Item](#delete-single-cart-item) -- [Delete All Cart Items](#delete-all-cart-items) +- [List Cart Items](#list-cart-items) +- [Single Cart Item](#single-cart-item) +- [Add Cart Item](#add-cart-item) +- [Edit Single Cart Item](#edit-single-cart-item) +- [Delete Single Cart Item](#delete-single-cart-item) +- [Delete All Cart Items](#delete-all-cart-items) ## List Cart Items @@ -29,6 +29,7 @@ curl "https://example-store.com/wp-json/wc/store/v1/cart/items" "key": "c74d97b01eae257e44aa9d5bade97baf", "id": 16, "quantity": 1, + "type": "simple", "quantity_limits": { "minimum": 1, "maximum": 1, @@ -108,6 +109,7 @@ curl "https://example-store.com/wp-json/wc/store/v1/cart/items" "key": "e03e407f41901484125496b5ec69a76f", "id": 29, "quantity": 1, + "type": "variation", "quantity_limits": { "minimum": 1, "maximum": 9999, diff --git a/tests/php/StoreApi/Routes/CartItems.php b/tests/php/StoreApi/Routes/CartItems.php index ca7ba6ace93..b963472acd1 100644 --- a/tests/php/StoreApi/Routes/CartItems.php +++ b/tests/php/StoreApi/Routes/CartItems.php @@ -82,6 +82,7 @@ public function test_get_items() { 0 => array( 'key' => $this->keys[0], 'id' => $this->products[0]->get_id(), + 'type' => $this->products[0]->get_type(), 'name' => $this->products[0]->get_name(), 'sku' => $this->products[0]->get_sku(), 'permalink' => $this->products[0]->get_permalink(), @@ -113,6 +114,7 @@ public function test_get_item() { array( 'key' => $this->keys[0], 'id' => $this->products[0]->get_id(), + 'type' => $this->products[0]->get_type(), 'name' => $this->products[0]->get_name(), 'sku' => $this->products[0]->get_sku(), 'permalink' => $this->products[0]->get_permalink(), @@ -256,6 +258,7 @@ public function test_prepare_item() { $this->assertArrayHasKey( 'key', $data ); $this->assertArrayHasKey( 'id', $data ); + $this->assertArrayHasKey( 'type', $data ); $this->assertArrayHasKey( 'quantity', $data ); $this->assertArrayHasKey( 'name', $data ); $this->assertArrayHasKey( 'sku', $data );