跳到主要内容

产品 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_idslug,以及可选的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 &ldquo;WordPress Pennant&rdquo; 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
属性类型是否必选描述
idinteger要检索的产品的 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 &ldquo;WordPress Pennant&rdquo; to your cart"
}
}

通过别名获取单个产品

通过别名获取单个产品。

GET /products/:slug
属性类型是否必选描述
slugstring用于检索产品的别名。
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"