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

Update cart API response and data store to contain product type #10825

Merged
merged 5 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
314 changes: 158 additions & 156 deletions src/StoreApi/docs/cart-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,162 +25,164 @@ curl "https://example-store.com/wp-json/wc/store/v1/cart/items"

```json
[
{
"key": "9bf31c7ff062936a96d3c8bd1f8f2ff3",
"id": 15,
"quantity": 1,
"quantity_limits": {
"minimum": 1,
"maximum": 99,
"multiple_of": 1,
"editable": true
},
"name": "Beanie",
"summary": "<p>This is a simple product.</p>",
"short_description": "<p>This is a simple product.</p>",
"description": "<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>",
"sku": "woo-beanie",
"low_stock_remaining": null,
"backorders_allowed": false,
"show_backorder_badge": false,
"sold_individually": false,
"permalink": "https://local.wordpress.test/product/beanie/",
"images": [
{
"id": 44,
"src": "https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2.jpg",
"thumbnail": "https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-324x324.jpg",
"srcset": "https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2.jpg 801w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-324x324.jpg 324w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-100x100.jpg 100w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-416x416.jpg 416w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-300x300.jpg 300w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-150x150.jpg 150w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-768x768.jpg 768w",
"sizes": "(max-width: 801px) 100vw, 801px",
"name": "beanie-2.jpg",
"alt": ""
}
],
"variation": [],
"item_data": [],
"prices": {
"currency_code": "GBP",
"currency_symbol": "£",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "£",
"currency_suffix": "",
"price": "1000",
"regular_price": "2000",
"sale_price": "1000",
"price_range": null,
"raw_prices": {
"precision": 6,
"price": "10000000",
"regular_price": "20000000",
"sale_price": "10000000"
}
},
"totals": {
"currency_code": "GBP",
"currency_symbol": "£",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "£",
"currency_suffix": "",
"line_subtotal": "1000",
"line_subtotal_tax": "0",
"line_total": "800",
"line_total_tax": "0"
},
"_links": {
"self": [
{
"href": "https://local.wordpress.test/wp-json/wc/store/v1/cart/items/9bf31c7ff062936a96d3c8bd1f8f2ff3"
}
],
"collection": [
{
"href": "https://local.wordpress.test/wp-json/wc/store/v1/cart/items"
}
]
}
},
{
"key": "e369853df766fa44e1ed0ff613f563bd",
"id": 34,
"quantity": 1,
"quantity_limits": {
"minimum": 1,
"maximum": 99,
"multiple_of": 1,
"editable": true
},
"name": "WordPress Pennant",
"summary": "<p>This is an external product.</p>",
"short_description": "<p>This is an external product.</p>",
"description": "<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>",
"sku": "wp-pennant",
"low_stock_remaining": null,
"backorders_allowed": false,
"show_backorder_badge": false,
"sold_individually": false,
"permalink": "https://local.wordpress.test/product/wordpress-pennant/",
"images": [
{
"id": 57,
"src": "https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1.jpg",
"thumbnail": "https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-324x324.jpg",
"srcset": "https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1.jpg 800w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-324x324.jpg 324w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-100x100.jpg 100w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-416x416.jpg 416w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-300x300.jpg 300w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-150x150.jpg 150w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-768x768.jpg 768w",
"sizes": "(max-width: 800px) 100vw, 800px",
"name": "pennant-1.jpg",
"alt": ""
}
],
"variation": [],
"item_data": [],
"prices": {
"currency_code": "GBP",
"currency_symbol": "£",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "£",
"currency_suffix": "",
"price": "1105",
"regular_price": "1105",
"sale_price": "1105",
"price_range": null,
"raw_prices": {
"precision": 6,
"price": "11050000",
"regular_price": "11050000",
"sale_price": "11050000"
}
},
"totals": {
"currency_code": "GBP",
"currency_symbol": "£",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "£",
"currency_suffix": "",
"line_subtotal": "1105",
"line_subtotal_tax": "0",
"line_total": "884",
"line_total_tax": "0"
},
"_links": {
"self": [
{
"href": "https://local.wordpress.test/wp-json/wc/store/v1/cart/items/e369853df766fa44e1ed0ff613f563bd"
}
],
"collection": [
{
"href": "https://local.wordpress.test/wp-json/wc/store/v1/cart/items"
}
]
}
}
{
"key": "9bf31c7ff062936a96d3c8bd1f8f2ff3",
"id": 15,
"type": "simple",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and line 109 are the only lines that changed in this file, not sure why the diff is showing the entire file.

"quantity": 1,
"quantity_limits": {
"minimum": 1,
"maximum": 99,
"multiple_of": 1,
"editable": true
},
"name": "Beanie",
"summary": "<p>This is a simple product.</p>",
"short_description": "<p>This is a simple product.</p>",
"description": "<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>",
"sku": "woo-beanie",
"low_stock_remaining": null,
"backorders_allowed": false,
"show_backorder_badge": false,
"sold_individually": false,
"permalink": "https://local.wordpress.test/product/beanie/",
"images": [
{
"id": 44,
"src": "https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2.jpg",
"thumbnail": "https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-324x324.jpg",
"srcset": "https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2.jpg 801w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-324x324.jpg 324w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-100x100.jpg 100w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-416x416.jpg 416w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-300x300.jpg 300w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-150x150.jpg 150w, https://local.wordpress.test/wp-content/uploads/2020/03/beanie-2-768x768.jpg 768w",
"sizes": "(max-width: 801px) 100vw, 801px",
"name": "beanie-2.jpg",
"alt": ""
}
],
"variation": [],
"item_data": [],
"prices": {
"currency_code": "GBP",
"currency_symbol": "£",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "£",
"currency_suffix": "",
"price": "1000",
"regular_price": "2000",
"sale_price": "1000",
"price_range": null,
"raw_prices": {
"precision": 6,
"price": "10000000",
"regular_price": "20000000",
"sale_price": "10000000"
}
},
"totals": {
"currency_code": "GBP",
"currency_symbol": "£",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "£",
"currency_suffix": "",
"line_subtotal": "1000",
"line_subtotal_tax": "0",
"line_total": "800",
"line_total_tax": "0"
},
"_links": {
"self": [
{
"href": "https://local.wordpress.test/wp-json/wc/store/v1/cart/items/9bf31c7ff062936a96d3c8bd1f8f2ff3"
}
],
"collection": [
{
"href": "https://local.wordpress.test/wp-json/wc/store/v1/cart/items"
}
]
}
},
{
"key": "e369853df766fa44e1ed0ff613f563bd",
"id": 34,
"type": "external",
"quantity": 1,
"quantity_limits": {
"minimum": 1,
"maximum": 99,
"multiple_of": 1,
"editable": true
},
"name": "WordPress Pennant",
"summary": "<p>This is an external product.</p>",
"short_description": "<p>This is an external product.</p>",
"description": "<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>",
"sku": "wp-pennant",
"low_stock_remaining": null,
"backorders_allowed": false,
"show_backorder_badge": false,
"sold_individually": false,
"permalink": "https://local.wordpress.test/product/wordpress-pennant/",
"images": [
{
"id": 57,
"src": "https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1.jpg",
"thumbnail": "https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-324x324.jpg",
"srcset": "https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1.jpg 800w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-324x324.jpg 324w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-100x100.jpg 100w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-416x416.jpg 416w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-300x300.jpg 300w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-150x150.jpg 150w, https://local.wordpress.test/wp-content/uploads/2020/03/pennant-1-768x768.jpg 768w",
"sizes": "(max-width: 800px) 100vw, 800px",
"name": "pennant-1.jpg",
"alt": ""
}
],
"variation": [],
"item_data": [],
"prices": {
"currency_code": "GBP",
"currency_symbol": "£",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "£",
"currency_suffix": "",
"price": "1105",
"regular_price": "1105",
"sale_price": "1105",
"price_range": null,
"raw_prices": {
"precision": 6,
"price": "11050000",
"regular_price": "11050000",
"sale_price": "11050000"
}
},
"totals": {
"currency_code": "GBP",
"currency_symbol": "£",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "£",
"currency_suffix": "",
"line_subtotal": "1105",
"line_subtotal_tax": "0",
"line_total": "884",
"line_total_tax": "0"
},
"_links": {
"self": [
{
"href": "https://local.wordpress.test/wp-json/wc/store/v1/cart/items/e369853df766fa44e1ed0ff613f563bd"
}
],
"collection": [
{
"href": "https://local.wordpress.test/wp-json/wc/store/v1/cart/items"
}
]
}
}
]
```

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