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

产品分类

The product categories API allows you to create, view, update, and delete individual, or a batch, of categories.

Product category properties

AttributeTypeDescription
idintegerUnique identifier for the resource. READ-ONLY
namestringCategory name. MANDATORY
slugstringAn alphanumeric identifier for the resource unique to its type.
parentintegerThe ID for the parent of the resource.
descriptionstringHTML description of the resource.
displaystringCategory archive display type. Options: default, products, subcategories and both. Default is default.
imageobjectImage data. See Product category - Image properties
menu_orderintegerMenu order, used to custom sort the resource.
countintegerNumber of published products for the resource. READ-ONLY

Product category - 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.

创建产品分类

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

POST /wp-json/wc/v3/products/categories

创建产品分类的例子:

curl -X POST https://example.com/wp-json/wc/v3/products/categories \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"name": "Clothing",
"image": {
"src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg"
}
}'

获取产品分类

此 API 允许您通过 ID 获取一个产品分类。

GET /wp-json/wc/v3/products/categories/<id>
curl https://example.com/wp-json/wc/v3/products/categories/9 \
-u consumer_key:consumer_secret

列出所有产品类别

此 API 允许您检索所有产品类别。

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

Available parameters

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.
excludearrayEnsure result set excludes specific ids.
includearrayLimit result set to specific ids.
orderstringOrder sort attribute ascending or descending. Options: asc and desc. Default is asc.
orderbystringSort collection by resource attribute. Options: id, include, name, slug, term_group, description and count. Default is name.
hide_emptybooleanWhether to hide resources not assigned to any products. Default is false.
parentintegerLimit result set to resources assigned to a specific parent.
productintegerLimit result set to resources assigned to a specific product.
slugstringLimit result set to resources with a specific slug.

Update a product category

This API lets you make changes to a product category.

PUT /wp-json/wc/v3/products/categories/<id>
curl -X PUT https://example.com/wp-json/wc/v3/products/categories/9 \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"description": "All kinds of clothes."
}'

删除一个产品分类

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

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

可用参数

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

批量更新产品分类

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

备注

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

POST /wp-json/wc/v3/products/categories/batch
curl -X POST https://example.com/wp-json/wc/v3/products/categories/batch \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"create": [
{
"name": "Albums"
},
{
"name": "Clothing"
}
],
"update": [
{
"id": 10,
"description": "Nice hoodies"
}
],
"delete": [
11,
12
]
}'