跳转到主要内容

产品 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

产品查询参数

以下参数可用于限制产品查询结果。

产品信息示例

以下是一个产品信息的示例,展示了如何获取和显示产品数据。

示例响应:

[
	{
		"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
属性 类型 是否必选 描述
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 &ldquo;WordPress Pennant&rdquo; 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"