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

产品

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

产品属性

AttributeTypeDescription
idintegerUnique identifier for the resource. READ-ONLY
namestringProduct name.
slugstringProduct slug.
permalinkstringProduct URL. READ-ONLY
date_createddate-timeThe date the product was created, in the site's timezone. READ-ONLY
date_created_gmtdate-timeThe date the product was created, as GMT. READ-ONLY
date_modifieddate-timeThe date the product was last modified, in the site's timezone. READ-ONLY
date_modified_gmtdate-timeThe date the product was last modified, as GMT. READ-ONLY
typestringProduct type. Options: simple, grouped, external and variable. Default is simple.
statusstringProduct status (post status). Options: draft, pending, private and publish. Default is publish.
featuredbooleanFeatured product. Default is false.
catalog_visibilitystringCatalog visibility. Options: visible, catalog, search and hidden. Default is visible.
descriptionstringProduct description.
short_descriptionstringProduct short description.
skustringUnique identifier.
global_unique_idstringGTIN, UPC, EAN or ISBN - a unique identifier for each distinct product and service that can be purchased.
pricestringCurrent product price. READ-ONLY
regular_pricestringProduct regular price.
sale_pricestringProduct 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.
price_htmlstringPrice formatted in HTML. READ-ONLY
on_salebooleanShows if the product is on sale. READ-ONLY
purchasablebooleanShows if the product can be bought. READ-ONLY
total_salesintegerAmount of sales. READ-ONLY
virtualbooleanIf the product is virtual. Default is false.
downloadablebooleanIf the product is downloadable. Default is false.
downloadsarrayList of downloadable files. See Product - 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.
external_urlstringProduct external URL. Only for external products.
button_textstringProduct external button text. Only for external products.
tax_statusstringTax status. Options: taxable, shipping and none. Default is taxable.
tax_classstringTax class.
manage_stockbooleanStock management at product level. 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 product is on backordered. READ-ONLY
sold_individuallybooleanAllow one item to be bought in a single order. Default is false.
weightstringProduct weight.
dimensionsobjectProduct dimensions. See Product - Dimensions properties
shipping_requiredbooleanShows if the product need to be shipped. READ-ONLY
shipping_taxablebooleanShows whether or not the product shipping is taxable. READ-ONLY
shipping_classstringShipping class slug.
shipping_class_idintegerShipping class ID. READ-ONLY
reviews_allowedbooleanAllow reviews. Default is true.
average_ratingstringReviews average rating. READ-ONLY
rating_countintegerAmount of reviews that the product have. READ-ONLY
related_idsarrayList of related products IDs. READ-ONLY
upsell_idsarrayList of up-sell products IDs.
cross_sell_idsarrayList of cross-sell products IDs.
parent_idintegerProduct parent ID.
purchase_notestringOptional note to send the customer after purchase.
categoriesarrayList of categories. See Product - Categories properties
tagsarrayList of tags. See Product - Tags properties
brandsarrayList of product brands. See Brands Properties. In write-mode pass an array of brand objects with id property (uses wp_set_object_terms())
imagesarrayList of images. See Product - Images properties
attributesarrayList of attributes. See Product - Attributes properties
default_attributesarrayDefaults variation attributes. See Product - Default attributes properties
variationsarrayList of variations IDs. READ-ONLY
grouped_productsarrayList of grouped products ID.
menu_orderintegerMenu order, used to custom sort products.
meta_dataarrayMeta data. See Product - Meta data properties

Product - Downloads properties

AttributeTypeDescription
idstringFile ID.
namestringFile name.
filestringFile URL.

Product - Dimensions properties

AttributeTypeDescription
lengthstringProduct length.
widthstringProduct width.
heightstringProduct height.

Product - Categories properties

AttributeTypeDescription
idintegerCategory ID.
namestringCategory name. READ-ONLY
slugstringCategory slug. READ-ONLY

Product - Tags properties

AttributeTypeDescription
idintegerTag ID.
namestringTag name. READ-ONLY
slugstringTag slug. READ-ONLY

Product - Images properties

AttributeTypeDescription
idintegerThe attachment ID from the Media Library.
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.

Brands Properties

AttributeTypeDescription
idintegerBrand ID REQUIRED FOR WRITE OPERATIONS
namestringBrand name READ-ONLY
slugstringBrand slug READ-ONLY

Product - Attributes properties

AttributeTypeDescription
idintegerAttribute ID.
namestringAttribute name.
positionintegerAttribute position.
visiblebooleanDefine if the attribute is visible on the "Additional information" tab in the product's page. Default is false.
variationbooleanDefine if the attribute can be used as variation. Default is false.
optionsarrayList of available term names of the attribute.

Product - Default attributes properties

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

Product - Meta data properties

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

创建产品

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

POST /wp-json/wc/v3/products
> 例子:如何创建一款包含一张现有图像和一张新的图像的简单产品:
curl -X POST https://example.com/wp-json/wc/v3/products \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"name": "Premium Quality",
"type": "simple",
"regular_price": "21.99",
"description": "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.",
"short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
"categories": [
{
"id": 9
},
{
"id": 14
}
],
"images": [
{
"id": 42
},
{
"src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg"
}
]
}'
> 例子:如何创建具有全局属性和非全局属性的“变量”产品:
curl -X POST https://example.com/wp-json/wc/v3/products \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"name": "Ship Your Idea",
"type": "variable",
"description": "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.",
"short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
"categories": [
{
"id": 9
},
{
"id": 14
}
],
"images": [
{
"src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_front.jpg"
},
{
"src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_back.jpg"
},
{
"src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_front.jpg"
},
{
"src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_3_back.jpg"
}
],
"attributes": [
{
"id": 6,
"position": 0,
"visible": false,
"variation": true,
"options": [
"Black",
"Green"
]
},
{
"name": "Size",
"position": 0,
"visible": true,
"variation": true,
"options": [
"S",
"M"
]
}
],
"default_attributes": [
{
"id": 6,
"option": "Black"
},
{
"name": "Size",
"option": "S"
}
]
}'

获取产品信息

此 API 允许您通过 ID 获取并查看特定产品的详细信息。

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

可选的查询参数: image_size (字符串)。使用已注册的特定图片尺寸来获取返回的图像 src 值的图片尺寸。如果请求的尺寸未注册,则默认为全尺寸。默认值为 full

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

列表所有产品

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

GET /wp-json/wc/v3/products
curl https://example.com/wp-json/wc/v3/products \
-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.
search_fieldsarrayFields to search when used with search parameter. All search tokens must be found across any of the specified fields. Takes precedence over other search parameters. If empty, uses default search behavior. Options: name, sku, global_unique_id, description, and short_description.
afterstringLimit response to resources published after a given ISO8601 compliant date.
beforestringLimit response to resources published before a given ISO8601 compliant date.
modified_afterstringLimit response to resources modified after a given ISO8601 compliant date.
modified_beforestringLimit response to resources modified after a given ISO8601 compliant date.
dates_are_gmtbooleanWhether to interpret dates as GMT when limiting response by published or modified date.
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, slug, price, popularity, rating, and menu_order. 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 products 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 products 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.
typestringLimit result set to products assigned a specific type. Options: simple, grouped, external and variable.
include_typesstringLimit result set to products with any of the types. Multiple statuses can be provided as a comma-separated list. Takes precedence over the type parameter. Options: simple, grouped, external and variable.
exclude_typesstringExclude products from result set with any of the specified types. Multiple statuses can be provided as a comma-separated list. Takes precedence over the include_types parameter. Options: simple, grouped, external and variable.
skustringLimit result set to products with a specific SKU.
featuredbooleanLimit result set to featured products.
categorystringLimit result set to products assigned a specific category ID.
tagstringLimit result set to products assigned a specific tag ID.
shipping_classstringLimit result set to products assigned a specific shipping class ID.
attributestringLimit result set to products with a specific attribute.
attribute_termstringLimit result set to products with a specific attribute term ID (required an assigned attribute).
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 products.
downloadablebooleanLimit result set to downloadable products.
image_sizestringUse a specific registered image size for the returned image src values. Falls back to the full size if the requested size is not registered. Default is full.

复制产品

此 API 可以帮助您复制一个产品。

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

更新产品

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

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

删除一个产品

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

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

可用参数

参数类型描述
force字符串使用 true 是否永久删除产品,默认值为 false

批量更新产品

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

备注

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

POST /wp-json/wc/v3/products/batch
curl -X POST https://example.com/wp-json/wc/v3/products/batch \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"create": [
{
"name": "Woo Single #1",
"type": "simple",
"regular_price": "21.99",
"virtual": true,
"downloadable": true,
"downloads": [
{
"name": "Woo Single",
"file": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_4_angle.jpg"
}
],
"categories": [
{
"id": 11
},
{
"id": 13
}
],
"images": [
{
"src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/cd_4_angle.jpg"
}
]
},
{
"name": "New Premium Quality",
"type": "simple",
"regular_price": "21.99",
"description": "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.",
"short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
"categories": [
{
"id": 9
},
{
"id": 14
}
],
"images": [
{
"src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg"
},
{
"src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg"
}
]
}
],
"update": [
{
"id": 799,
"default_attributes": [
{
"id": 6,
"name": "Color",
"option": "Green"
},
{
"id": 0,
"name": "Size",
"option": "M"
}
]
}
],
"delete": [
794
]
}'