跳到主要内容
将当前页面内容以 Markdown 格式复制到剪贴板

产品变体

产品变体 API 允许您创建、查看、更新和删除单个或一批产品变体。

产品变体的属性

AttributeTypeDescription
idintegerUnique identifier for the resource. READ-ONLY
date_createddate-timeThe date the variation was created, in the site's timezone. READ-ONLY
date_created_gmtdate-timeThe date the variation was created, as GMT. READ-ONLY
date_modifieddate-timeThe date the variation was last modified, in the site's timezone. READ-ONLY
date_modified_gmtdate-timeThe date the variation was last modified, as GMT. READ-ONLY
descriptionstringVariation description.
permalinkstringVariation URL. READ-ONLY
skustringUnique identifier.
global_unique_idstringGTIN, UPC, EAN or ISBN. A global unique identifier for the variation.
pricestringCurrent variation price. READ-ONLY
regular_pricestringVariation regular price.
sale_pricestringVariation sale price.
date_on_sale_fromdate-timeStart date of sale price, in the site's timezone.
date_on_sale_from_gmtdate-timeStart date of sale price, as GMT.
date_on_sale_todate-timeEnd date of sale price, in the site's timezone.
date_on_sale_to_gmtdate-timeEnd date of sale price, as GMT.
on_salebooleanShows if the variation is on sale. READ-ONLY
statusstringVariation status. Options: draft, pending, private and publish. Default is publish.
purchasablebooleanShows if the variation can be bought. READ-ONLY
virtualbooleanIf the variation is virtual. Default is false.
downloadablebooleanIf the variation is downloadable. Default is false.
downloadsarrayList of downloadable files. See Product variation - Downloads properties
download_limitintegerNumber of times downloadable files can be downloaded after purchase. Default is -1.
download_expiryintegerNumber of days until access to downloadable files expires. Default is -1.
tax_statusstringTax status. Options: taxable, shipping and none. Default is taxable.
tax_classstringTax class.
manage_stockboolean, stringStock management at variation level. Possible values are either a boolean or parent. Default is false.
stock_quantityintegerStock quantity.
stock_statusstringControls the stock status of the product. Options: instock, outofstock, onbackorder. Default is instock.
backordersstringIf managing stock, this controls if backorders are allowed. Options: no, notify and yes. Default is no.
backorders_allowedbooleanShows if backorders are allowed. READ-ONLY
backorderedbooleanShows if the variation is on backordered. READ-ONLY
weightstringVariation weight.
dimensionsobjectVariation dimensions. See Product variation - Dimensions properties
shipping_classstringShipping class slug.
shipping_class_idstringShipping class ID. READ-ONLY
imageobjectVariation image data. See Product variation - Image properties
gallery_image_idsarrayVariation gallery image IDs, excluding the featured image (which is set via image).
attributesarrayList of attributes. See Product variation - Attributes properties
menu_orderintegerMenu order, used to custom sort products.
meta_dataarrayMeta data. See Product variation - Meta data properties

Product variation - Downloads properties

AttributeTypeDescription
idstringFile ID.
namestringFile name.
filestringFile URL.

Product variation - Dimensions properties

AttributeTypeDescription
lengthstringVariation length.
widthstringVariation width.
heightstringVariation height.

Product variation - Image properties

AttributeTypeDescription
idintegerImage ID.
date_createddate-timeThe date the image was created, in the site's timezone. READ-ONLY
date_created_gmtdate-timeThe date the image was created, as GMT. READ-ONLY
date_modifieddate-timeThe date the image was last modified, in the site's timezone. READ-ONLY
date_modified_gmtdate-timeThe date the image was last modified, as GMT. READ-ONLY
srcstringImage URL.
namestringImage name.
altstringImage alternative text.

Product variation - Attributes properties

AttributeTypeDescription
idintegerAttribute ID.
namestringAttribute name.
optionstringSelected attribute term name.

Product variation - Meta data properties

AttributeTypeDescription
idintegerMeta ID. READ-ONLY
keystringMeta key.
valuestringMeta value.

创建产品变体

这个 API 可以帮助您创建新的产品变体。

POST /wp-json/wc/v3/products/<product_id>/variations
curl -X POST https://example.com/wp-json/wc/v3/products/22/variations \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"regular_price": "9.00",
"image": {
"id": 423
},
"attributes": [
{
"id": 6,
"option": "Black"
}
]
}'

获取产品变体

此 API 允许您通过编号检索并查看特定的产品变体。

GET /wp-json/wc/v3/products/<product_id>/variations/<id>

可选的查询参数: image_size (字符串)。用于返回的变体图片的 src 属性时,使用指定的已注册图片尺寸。如果请求的尺寸未注册,则回退到全尺寸。默认值为 full

curl https://example.com/wp-json/wc/v3/products/22/variations/732 \
-u consumer_key:consumer_secret

列出所有产品变体

此 API 帮助您查看所有产品变体。

GET /wp-json/wc/v3/products/<product_id>/variations
curl https://example.com/wp-json/wc/v3/products/22/variations \
-u consumer_key:consumer_secret

可用的参数

ParameterTypeDescription
contextstringScope under which the request is made; determines fields present in response. Options: view and edit. Default is view.
pageintegerCurrent page of the collection. Default is 1.
per_pageintegerMaximum number of items to be returned in result set. Default is 10.
searchstringLimit results to those matching a string.
afterstringLimit response to resources published after a given ISO8601 compliant date.
beforestringLimit response to resources published before a given ISO8601 compliant date.
dates_are_gmtbooleanInterpret after and before as UTC dates when true.
excludearrayEnsure result set excludes specific IDs.
includearrayLimit result set to specific ids.
offsetintegerOffset the result set by a specific number of items.
orderstringOrder sort attribute ascending or descending. Options: asc and desc. Default is desc.
orderbystringSort collection by object attribute. Options: date, modified, id, include, title and slug. Default is date.
parentarrayLimit result set to those of particular parent IDs.
parent_excludearrayLimit result set to all items except those of a particular parent ID.
slugstringLimit result set to products with a specific slug.
statusstringLimit result set to products assigned a specific status. Options: any, draft, pending, private and publish. Default is any.
include_statusstringLimit result set to product variations with any of the specified statuses. Multiple statuses can be provided as a comma-separated list. Takes precedence over the status parameter. Options: any, future, trash, draft, pending, private, and publish.
exclude_statusstringExclude product variations from result set with any of the specified statuses. Multiple statuses can be provided as a comma-separated list. Takes precedence over the include_status parameter. Options: future, trash, draft, pending, private, and publish.
skustringLimit result set to products with a specific SKU.
tax_classstringLimit result set to products with a specific tax class. Default options: standard, reduced-rate and zero-rate.
on_salebooleanLimit result set to products on sale.
min_pricestringLimit result set to products based on a minimum price.
max_pricestringLimit result set to products based on a maximum price.
stock_statusstringLimit result set to products with specified stock status. Options: instock, outofstock and onbackorder.
virtualbooleanLimit result set to virtual product variations
downloadablebooleanLimit result set to downloadable product variations.
image_sizestringUse a specific registered image size for the returned variation image src. Falls back to the full size if the requested size is not registered. Default is full.

更新产品变体

此 API 允许您对产品变体进行更改。

PUT /wp-json/wc/v3/products/<product_id>/variations/<id>
curl -X PUT https://example.com/wp-json/wc/v3/products/22/variations/733 \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"regular_price": "10.00"
}'

删除产品变体

这个 API 可以帮助您删除一个产品变体。

DELETE /wp-json/wc/v3/products/<product_id>/variations/<id>
curl -X DELETE https://example.com/wp-json/wc/v3/products/22/variations/733?force=true \
-u consumer_key:consumer_secret

可用参数

参数类型描述
force字符串必须为 true,因为资源不支持放入回收站。

批量更新产品变体

此 API 可以帮助您批量创建、更新和删除多个产品变体。

备注

注意:默认情况下,限制为最多可以创建、更新或删除 100 个对象。

POST /wp-json/wc/v3/products/<product_id>/variations/batch
curl -X POST https://example.com/wp-json/wc/v3/products/22/variations/batch \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"create": [
{
"regular_price": "10.00",
"attributes": [
{
"id": 6,
"option": "Blue"
}
]
},
{
"regular_price": "10.00",
"attributes": [
{
"id": 6,
"option": "White"
}
]
}
],
"update": [
{
"id": 733,
"regular_price": "10.00"
}
],
"delete": [
732
]
}'