Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Update cart API response and data store to contain product type (#10825)
Browse files Browse the repository at this point in the history
  • Loading branch information
opr authored and senadir committed Oct 19, 2023
1 parent ea812d3 commit 0df2a0e
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 6 deletions.
2 changes: 2 additions & 0 deletions assets/js/previews/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
Expand Down Expand Up @@ -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' ),
Expand Down
1 change: 1 addition & 0 deletions assets/js/types/type-defs/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
1 change: 1 addition & 0 deletions src/StoreApi/Schemas/V1/CartItemSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() ),
Expand Down
6 changes: 6 additions & 0 deletions src/StoreApi/Schemas/V1/ItemSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
14 changes: 8 additions & 6 deletions src/StoreApi/docs/cart-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

## Table of Contents <!-- omit in toc -->

- [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

Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions tests/php/StoreApi/Routes/CartItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit 0df2a0e

Please sign in to comment.