购物车项目 API
本 API 提供了访问和管理购物车项目的接口。
功能描述:
该 API 允许您:
- 获取购物车中的所有项目: 可以根据用户 ID 或购物车 ID 获取指定购物车中的所有项目列表。
- 添加项目到购物车: 可以向指定购物车中添加新的项目。
- 更新购物车中的项目: 可以修改购物车中现有项目的数量或价格。
- 从购物车中删除项目: 可以从指定购物车中移除特定的项目。
API 端点:
GET /api/cart/%s/items: 获取指定购物车中的所有项目。POST /api/cart/%s/items: 向指定购物车中添加新的项目。PUT /api/cart/%s/items/%d: 更新指定购物车中的项目。DELETE /api/cart/%s/items/%d: 从指定购物车中删除项目。
请求参数:
| 参数名 | 类型 | 描述 | 必填 |
|---|---|---|---|
| cart_id | string | 购物车 ID | 是 |
| item_id | integer | 项目 ID | 是 |
| quantity | integer | 项目数量 | 否 |
| price | float | 项目价格 | 否 |
| name | string | 项目名称 | 否 |
| description | string | 项目描述 | 否 |
响应格式:
{
"status": "success",
"data": [
{
"item_id": %d,
"name": "{name}",
"quantity": %d,
"price": %.2f
}
]
}
错误处理:
如果发生错误,API 将返回以下错误代码:
400 Bad Request: 请求参数无效。404 Not Found: 购物车或项目未找到。500 Internal Server Error: 服务器内部错误。
示例:
-
获取购物车中的所有项目:
curl -X GET http://localhost:8000/api/cart/user123/items -
向购物车中添加新的项目:
curl -X POST http://localhost:8000/api/cart/user123/items \-H "Content-Type: application/json" \-d '{"item_id": 1,"quantity": 2,"price": 10.00}'
注意:
- 请确保您已正确配置 API 密钥,以便访问该 API。
- 请参考 API 文档以获取更多详细信息。
购物车项目列表
GET /cart/items
使用此接口不需要额外的参数。
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,
"multiple_of": 1,
"editable": false
},
"name": "Beanie",
"short_description": "<p>这是一个简单的产品。</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": true,
"permalink": "https://store.local/product/beanie/",
"images": [
{
"id": 45,
"src": "https://store.local/wp-content/uploads/2023/01/beanie-2.jpg",
"thumbnail": "https://store.local/wp-content/uploads/2023/01/beanie-2-450x450.jpg",
"srcset": "https://store.local/wp-content/uploads/2023/01/beanie-2.jpg 801w, https://store.local/wp-content/uploads/2023/01/beanie-2-450x450.jpg 450w, https://store.local/wp-content/uploads/2023/01/beanie-2-100x100.jpg 100w, https://store.local/wp-content/uploads/2023/01/beanie-2-600x600.jpg 600w, https://store.local/wp-content/uploads/2023/01/beanie-2-300x300.jpg 300w, https://store.local/wp-content/uploads/2023/01/beanie-2-150x150.jpg 150w, https://store.local/wp-content/uploads/2023/01/beanie-2-768x768.jpg 768w",
"sizes": "(max-width: 801px) 100vw, 801px",
"name": "beanie-2.jpg",
"alt": ""
}
],
"variation": [],
"item_data": [],
"prices": {
"price": "1800",
"regular_price": "2000",
"sale_price": "1800",
"price_range": null,
"currency_code": "USD",
"currency_symbol": "$",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "$",
"currency_suffix": "",
"raw_prices": {
"precision": 6,
"price": "18000000",
"regular_price": "20000000",
"sale_price": "18000000"
}
},
"totals": {
"line_subtotal": "1800",
"line_subtotal_tax": "360",
"line_total": "1800",
"line_total_tax": "360",
"currency_code": "USD",
"currency_symbol": "$",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "$",
"currency_suffix": ""
},
"catalog_visibility": "visible",
"extensions": {},
"_links": {
"self": [
{
"href": "https://store.local/wp-json/wc/store/v1/cart/items/c74d97b01eae257e44aa9d5bade97baf"
}
],
"collection": [
{
"href": "https://store.local/wp-json/wc/store/v1/cart/items"
}
]
}
},
{
"key": "e03e407f41901484125496b5ec69a76f",
"id": 29,
"quantity": 1,
"type": "variation",
"quantity_limits": {
"minimum": 1,
"maximum": 9999,
"multiple_of": 1,
"editable": true
},
"name": "Hoodie",
"short_description": "",
"description": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum sagittis orci ac odio dictum tincidunt. Donec ut metus leo. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed luctus, dui eu sagittis sodales, nulla nibh sagittis augue, vel porttitor diam enim non metus. Vestibulum aliquam augue neque. Phasellus tincidunt odio eget ullamcorper efficitur. Cras placerat ut turpis pellentesque vulputate. Nam sed consequat tortor. Curabitur finibus sapien dolor. Ut eleifend tellus nec erat pulvinar dignissim. Nam non arcu purus. Vivamus et massa massa.</p>",
"sku": "woo-hoodie-red",
"low_stock_remaining": null,
"backorders_allowed": false,
"show_backorder_badge": false,
"sold_individually": false,
"permalink": "https://store.local/product/hoodie/?attribute_pa_color=red&attribute_logo=No",
"images": [
{
"id": 40,
"src": "https://store.local/wp-content/uploads/2023/01/hoodie-2.jpg",
"thumbnail": "https://store.local/wp-content/uploads/2023/01/hoodie-2-450x450.jpg",
"srcset": "https://store.local/wp-content/uploads/2023/01/hoodie-2.jpg 801w, https://store.local/wp-content/uploads/2023/01/hoodie-2-450x450.jpg 450w, https://store.local/wp-content/uploads/2023/01/hoodie-2-100x100.jpg 100w, https://store.local/wp-content/uploads/2023/01/hoodie-2-600x600.jpg 600w, https://store.local/wp-content/uploads/2023/01/hoodie-2-300x300.jpg 300w, https://store.local/wp-content/uploads/2023/01/hoodie-2-150x150.jpg 150w, https://store.local/wp-content/uploads/2023/01/hoodie-2-768x768.jpg 768w",
"sizes": "(max-width: 801px) 100vw, 801px",
"name": "hoodie-2.jpg",
"alt": ""
}
],
"variation": [
{
"raw_attribute": "attribute_pa_color",
"attribute": "颜色",
"value": "红色"
},
{
"raw_attribute": "attribute_logo",
"attribute": "徽章",
"value": "无"
}
],
"item_data": [],
"prices": {
"price": "4200",
"regular_price": "4500",
"sale_price": "4200",
"price_range": null,
"currency_code": "USD",
"currency_symbol": "$",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "$",
"currency_suffix": "",
"raw_prices": {
"precision": 6,
"price": "42000000",
"regular_price": "45000000",
"sale_price": "42000000"
}
},
"totals": {
"line_subtotal": "4200",
"line_subtotal_tax": "840",
"line_total": "4200",
"line_total_tax": "840",
"currency_code": "USD",
"currency_symbol": "$",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "$",
"currency_suffix": ""
},
"catalog_visibility": "visible",
"extensions": {},
"_links": {
"self": [
{
"href": "https://store.local/wp-json/wc/store/v1/cart/items/e03e407f41901484125496b5ec69a76f"
}
],
"collection": [
{
"href": "https://store.local/wp-json/wc/store/v1/cart/items"
}
]
}
}
]
单个购物车项目
通过其关键字获取单个购物车项目。
GET /cart/items/:key
| 属性 | 类型 | 必填 | 描述 |
|---|---|---|---|
key | 字符串 | 是 | 要检索的购物车项目的关键字。 |
curl "https://example-store.com/wp-json/wc/store/v1/cart/items/c74d97b01eae257e44aa9d5bade97baf"
例子回复:
{
"key": "c74d97b01eae257e44aa9d5bade97baf",
"id": 16,
"quantity": 1,
"quantity_limits": {
"minimum": 1,
"maximum": 1,
"multiple_of": 1,
"editable": false
},
"name": "Beanie",
"short_description": "<p>这是一个简单产品。</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": true,
"permalink": "https://store.local/product/beanie/",
"images": [
{
"id": 45,
"src": "https://store.local/wp-content/uploads/2023/01/beanie-2.jpg",
"thumbnail": "https://store.local/wp-content/uploads/2023/01/beanie-2-450x450.jpg",
"srcset": "https://store.local/wp-content/uploads/2023/01/beanie-2.jpg 801w, https://store.local/wp-content/uploads/2023/01/beanie-2-450x450.jpg 450w, https://store.local/wp-content/uploads/2023/01/beanie-2-100x100.jpg 100w, https://store.local/wp-content/uploads/2023/01/beanie-2-600x600.jpg 600w, https://store.local/wp-content/uploads/2023/01/beanie-2-300x300.jpg 300w, https://store.local/wp-content/uploads/2023/01/beanie-2-150x150.jpg 150w, https://store.local/wp-content/uploads/2023/01/beanie-2-768x768.jpg 768w",
"sizes": "(max-width: 801px) 100vw, 801px",
"name": "beanie-2.jpg",
"alt": ""
}
],
"variation": [],
"item_data": [],
"prices": {
"price": "1800",
"regular_price": "2000",
"sale_price": "1800",
"price_range": null,
"currency_code": "USD",
"currency_symbol": "$",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "$",
"currency_suffix": "",
"raw_prices": {
"precision": 6,
"price": "18000000",
"regular_price": "20000000",
"sale_price": "18000000"
}
},
"totals": {
"line_subtotal": "1800",
"line_subtotal_tax": "360",
"line_total": "1800",
"line_total_tax": "360",
"currency_code": "USD",
"currency_symbol": "$",
"currency_minor_unit": 2,
"currency_decimal_separator": ".",
"currency_thousand_separator": ",",
"currency_prefix": "$",
"currency_suffix": ""
},
"catalog_visibility": "visible",
"extensions": {},
"_links": {
"self": [
{
"href": "https://store.local/wp-json/wc/store/v1/cart/items/(?P<key>[\\w-]{32})/c74d97b01eae257e44aa9d5bade97baf"
}
],
"collection": [
{
"href": "https://store.local/wp-json/wc/store/v1/cart/items/(?P<key>[\\w-]{32})"
}
]
}
}
添加购物车项目
将项目添加到购物车。返回已添加的新购物车项目,或错误响应。
POST /cart/items/
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
id | 整数 | 是 | 购物车项目的产品或变体 ID。 |
quantity | 整数 | 是 | 购物车中此项目的数量。 |
variation | 数组 | 是 | 选择的属性(用于变体),包含一个对象数组,其中包含键 attribute 和 value。 |
curl --request POST https://example-store.com/wp-json/wc/store/v1/cart/items?id=100&quantity=1
有关示例响应,请参阅 单个购物车项目。
如果您想一次性添加多个项目到购物车,请参阅 批量操作。
编辑单个购物车项目
编辑购物车中的项目。
PUT /cart/items/:key
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
key | 字符串 | 是 | 要编辑的购物车项目的键。 |
quantity | 整数 | 是 | 购物车中此项目的数量。 |
curl --request PUT https://example-store.com/wp-json/wc/store/v1/cart/items/e369853df766fa44e1ed0ff613f563bd?quantity=10
有关示例响应,请参阅 单个购物车项目。
删除单个购物车项目
通过其键从购物车中删除项目。
DELETE /cart/items/:key
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
key | 字符串 | 是 | 要编辑的购物车项目的键。 |
curl --request DELETE https://example-store.com/wp-json/wc/store/v1/cart/items/e369853df766fa44e1ed0ff613f563bd
删除所有购物车项目
一次性从购物车中删除所有项目。
DELETE /cart/items/
使用此端点不需要额外的参数。
curl --request DELETE https://example-store.com/wp-json/wc/store/v1/cart/items
示例响应:
[]