产品 API
该商店产品 API 提供公开的产品数据,以便在客户端进行渲染。
列表产品
GET /products
GET /products?search=产品名称
GET /products?slug=别名-1,别名-2
GET /products?after=2017-03-22&date_column=日期
GET /products?before=2017-03-22&date_column=日期
GET /products?exclude=10,44,33
GET /products?include=10,44,33
GET /products?offset=10
GET /products?order=asc&orderby=价格
GET /products?parent=10
GET /products?parent_exclude=10
GET /products?type=simple
GET /products?sku=sku-1,sku-2
GET /products?featured=true
GET /products?category=22
GET /products?brand=adidas
GET /products?_unstable_tax_my-taxonomy=my-taxonomy-term-id
GET /products?tag=special-items
GET /products?attributes[0][attribute]=pa_color&attributes[0][slug]=red
GET /products?on_sale=true
GET /products?min_price=5000
GET /products?max_price=10000
GET /products?stock_status=['outofstock']
GET /products?catalog_visibility=search
GET /products?rating=4,5
GET /products?related=34
GET /products?return_price_range=true
GET /products?return_attribute_counts=pa_size,pa_color
GET /products?return_rating_counts=true
产品查询参数
以下参数可用于限制产品查询结果。
| 属性 | 类型 | 是否必填 | 描述 |
|---|---|---|---|
search | 字符串 | 否 | 限制结果,仅包含匹配的字符串。 |
slug | 字符串 | 否 | 限制结果集,仅包含具有特定别名(s)的产品。 使用逗号分隔。 |
after | 字符串 | 否 | 限制响应,仅包含在给定符合ISO8601标准的日期之后创建的资源。 |
before | 字符串 | 否 | 限制响应,仅包含在给定符合ISO8601标准的日期之前创建的资源。 |
date_column | 字符串 | 否 | 当限制响应时,用于比较的日期列。 允许的值:date, date_gmt, modified, modified_gmt |
exclude | 数组 | 否 | 确保结果集排除特定的ID。 |
include | 数组 | 否 | 限制结果集,仅包含特定的ID。 |
offset | 整数 | 否 | 偏移结果集,从指定的项目数开始。 |
order | 字符串 | 否 | 指定排序属性的升序或降序。 允许的值:asc, desc |
orderby | 字符串 | 否 | 按照对象属性对集合进行排序。 允许的值:date, modified, id, include, title, slug, price, popularity, rating, menu_order, comment_count |
parent | 数组 | 否 | 限制结果集,仅包含特定父ID的产品。 |
parent_exclude | 数组 | 否 | 限制结果集,排除所有具有特定父ID的产品。 |
type | 字符串 | 否 | 限制结果集,仅包含分配了特定类型的的产品。 |
sku | 字符串 | 否 | 限制结果集,仅包含具有特定SKU(s)的产品。 使用逗号分隔。 |
featured | 布尔值 | 否 | 限制结果集,仅包含推荐的产品。 |
category | 字符串 | 否 | 限制结果集,仅包含分配到分类ID或别名(s)的产品,用逗号分隔。 |
category_operator | 字符串 | 否 | 用于比较产品分类的运算符。 允许的值:in, not_in, and |
brand | 字符串 | 否 | 限制结果集,仅包含分配到品牌ID或别名(s)的产品,用逗号分隔。 |
brand_operator | 字符串 | 否 | 用于比较产品品牌术语的运算符。 允许的值:in, not_in, and |
_unstable_tax_[product-taxonomy] | 字符串 | 否 | 限制结果集,仅包含分配到自定义产品分类术语ID的产品。 [product-taxonomy] 应该是已注册的自定义产品分类的键。 |
_unstable_tax_[product-taxonomy]_operator | 字符串 | 否 | 用于比较自定义产品分类术语的运算符。 允许的值:in, not_in, and |
tag | 字符串 | 否 | 限制结果集,仅包含分配了特定标签ID的产品。 |
tag_operator | 字符串 | 否 | 用于比较产品标签的运算符。 允许的值:in, not_in, and |
on_sale | 布尔值 | 否 | 限制结果集,仅包含正在促销的产品。 |
min_price | 字符串 | 否 | 限制结果集,仅包含基于最小价格的产品,价格使用货币的最小单位表示。 例如,对于使用两位小数的货币(如USD),提供10025表示100.25,对于使用零小数的货币(如JPY),提供1025表示1025。 |
max_price | 字符串 | 否 | 限制结果集,仅包含基于最大价格的产品,价格使用货币的最小单位表示。 例如,对于使用两位小数的货币(如USD),提供10025表示100.25,对于使用零小数的货币(如JPY),提供1025表示1025。 |
stock_status | 数组 | 否 | 限制结果集,仅包含具有指定库存状态的产品。 期望的是一个包含字符串的数组,字符串可以是'instock'、'outofstock'或'onbackorder'。 |
attributes | 数组 | 否 | 限制结果集,仅包含特定属性术语的产品。 期望的是一个对象数组,每个对象包含attribute(分类),term_id或slug,以及可选的operator用于比较。 |
attribute_relation | 字符串 | 否 | 当同时过滤多个属性时,属性之间的逻辑关系。 |
catalog_visibility | 字符串 | 否 | 确定是否显示隐藏或可见的目录产品。 允许的值:any, visible, catalog, search, hidden |
rating | 数组 | 否 | 限制结果集,仅包含具有特定平均评分的产品。 允许的值:1, 2, 3, 4, 5. |
related | 整数 | 否 | 限制结果集,仅包含与特定产品ID相关的产品。 |
产品信息示例
以下是一个产品信息的示例,展示了如何获取和显示产品数据。
示例响应:
[
{
"id": 34,
"name": "WordPress Pennant",
"slug": "wordpress-pennant",
"variation": "",
"permalink": "https://local.wordpress.test/product/wordpress-pennant/",
"sku": "wp-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>",
"on_sale": false,
"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
},
"average_rating": "0",
"review_count": 0,
"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": ""
}
],
"has_options": false,
"is_purchasable": true,
"is_in_stock": true,
"low_stock_remaining": null,
"add_to_cart": {
"text": "Add to cart",
"description": "Add “WordPress Pennant” to your cart"
}
}
]
字段说明:
id: 产品ID。name: 产品名称。slug: 产品的别名。variation: 产品的变体。permalink: 产品的固定链接。sku: 产品的库存单位。summary: 产品的摘要。short_description: 产品的简短描述。description: 产品的详细描述。on_sale: 是否正在促销。prices: 价格信息,包括货币代码、符号、小数位数、分隔符、前缀、后缀、价格、原价、折扣价和价格范围。average_rating: 平均评分。review_count: 评论数量。images: 产品图像列表。has_options: 是否有选项。is_purchasable: 是否可购买。is_in_stock: 是否有库存。low_stock_remaining: 库存剩余量(如果库存不足)。add_to_cart: "加入购物车"按钮的文本和描述。
如何获取产品信息:
可以使用以下 curl 命令从商店获取产品信息:
curl "https://example-store.com/wp-json/wc/store/v1/products"
示例:
Permalink: 固定链接Variation: 变体Link: 链接Slug: 别名External Product: 外部产品Add to Cart: 加入购物车Remaining: 剩余Currency: 货币Summary: 摘要Product: 产品Images: 图像Upload: 上传Prefix: 前缀Option: 选项Suffix: 后缀Width: 宽度Stock: 库存range: 范围Store: 商店Done: 完成
通过 ID 获取单个产品
获取指定 ID 的单个产品。
GET /products/:id
| 属性 | 类型 | 是否必选 | 描述 |
|---|---|---|---|
id | integer | 是 | 要检索的产品的 ID。 |
curl "https://example-store.com/wp-json/wc/store/v1/products/34"
示例响应:
{
"id": 34,
"name": "WordPress Pennant",
"slug": "wordpress-pennant",
"variation": "",
"permalink": "https://local.wordpress.test/product/wordpress-pennant/",
"sku": "wp-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>",
"on_sale": false,
"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
},
"average_rating": "0",
"review_count": 0,
"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": ""
}
],
"has_options": false,
"is_purchasable": true,
"is_in_stock": true,
"low_stock_remaining": null,
"add_to_cart": {
"text": "Add to cart",
"description": "Add “WordPress Pennant” to your cart"
}
}
通过别名获取单个产品
通过别名获取单个产品。
GET /products/:slug
| 属性 | 类型 | 是否必选 | 描述 |
|---|---|---|---|
slug | string | 是 | 用于检索产品的别名。 |
curl "https://example-store.com/wp-json/wc/store/v1/products/wordpress-pennant"
示例响应:
{
"id": 34,
"name": "WordPress Pennant",
"slug": "wordpress-pennant",
"variation": "",
"permalink": "https://local.wordpress.test/product/wordpress-pennant/",
"sku": "wp-pennant",
"summary": "<p>这是一个外部产品。</p>",
"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>",
"on_sale": false,
"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
},
"average_rating": "0",
"review_count": 0,
"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": ""
}
],
"has_options": false,
"is_purchasable": true,
"is_in_stock": true,
"low_stock_remaining": null,
"add_to_cart": {
"text": "加入购物车",
"description": "将“WordPress Pennant”添加到您的购物车"
}
}
产品链接和嵌入
产品响应包含 _links,其中提供指向相关资源的 URL。当产品配置了追加销售、交叉销售或相关产品时,会包含可嵌入的链接,这些链接可与 WordPress 的 _embed 功能一起使用。
可用的链接
| 链接 | 描述 | 可嵌入 |
|---|---|---|
self | 链接至当前产品 | No |
collection | 链接至产品集合 | No |
up | 链接至父产品 (用于变体) | No |
upsells | 链接用于获取追加销售产品 (如果已配置) | Yes |
cross_sells | 链接用于获取交叉销售产品 (如果已配置) | Yes |
related | 链接用于获取相关产品 | Yes |
包含链接的示例响应
{
"id": 34,
"name": "WordPress Pennant",
"_links": {
"self": [{"href": "https://local.wordpress.test/wp-json/wc/store/v1/products/34"}],
"collection": [{"href": "https://local.wordpress.test/wp-json/wc/store/v1/products"}],
"upsells": [{"href": "https://local.wordpress.test/wp-json/wc/store/v1/products?include=10,20", "embeddable": true}],
"cross_sells": [{"href": "https://local.wordpress.test/wp-json/wc/store/v1/products?include=30", "embeddable": true}],
"related": [{"href": "https://local.wordpress.test/wp-json/wc/store/v1/products?related=34&per_page=10", "embeddable": true}]
}
}
使用 _embed 参数
向任何产品请求添加 ?_embed,即可自动获取并包含链接的资源,并将它们嵌入到 _embedded 对象中:
curl "https://local.wordpress.test/wp-json/wc/store/v1/products/34?_embed"
包含嵌入的示例响应:
{
"id": 34,
"name": "WordPress Pennant",
"_links": {
"self": [{"href": "https://local.wordpress.test/wp-json/wc/store/v1/products/34"}],
"collection": [{"href": "https://local.wordpress.test/wp-json/wc/store/v1/products"}],
"upsells": [{"href": "https://local.wordpress.test/wp-json/wc/store/v1/products?include=10,20", "embeddable": true}]
},
"_embedded": {
"upsells": [
{"id": 10, "name": "追加销售产品 1", "...": "..."},
{"id": 20, "name": "追加销售产品 2", "...": "..."}
]
}
}
产品变体
默认情况下,商店 API 排除产品变体。 您可以使用 type=variation 参数来检索产品的变体。
curl "https://example-store.com/wp-json/wc/store/v1/products?type=variation"