将当前页面内容以 Markdown 格式复制到剪贴板
产品属性项
产品属性术语 API 允许您创建、查看、更新和删除单个或批量属性术语。
产品属性术语属性
| 属性 | 类型 | 描述 |
|---|---|---|
id | integer | 资源的唯一标识符。只读 |
name | string | 术语名称。必填 |
slug | string | 用于标识资源的字母数字标识符,该标识符在资源类型中是唯一的。 |
description | string | 资源的 HTML 描述。 |
menu_order | integer | 菜单顺序,用于自定义排序资源。 |
count | integer | 该资源已发布的产品的数量。只读 |
创建一个属性术语
此 API 可帮助您创建新的产品属性术语。
POST /wp-json/wc/v3/products/attributes/<attribute_id>/terms
- cURL
- JavaScript
- PHP
- Python
- Ruby
- JSON Response
curl -X POST https://example.com/wp-json/wc/v3/products/attributes/2/terms \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"name": "XXS"
}'
const data = {
name: 'XXS',
};
WooCommerce.post( 'products/attributes/2/terms', data )
.then( ( response ) => {
console.log( response.data );
} )
.catch( ( error ) => {
console.log( error.response.data );
} );
<?php
$data = [
'name' => 'XXS'
];
print_r($woocommerce->post('products/attributes/2/terms', $data));
?>
data = {
"name": "XXS"
}
print(wcapi.post("products/attributes/2/terms", data).json())
data = {
name: "XXS"
}
woocommerce.post("products/attributes/2/terms", data).parsed_response
{
"id": 23,
"name": "XXS",
"slug": "xxs",
"description": "",
"menu_order": 1,
"count": 1,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms/23"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms"
}
]
}
}
获取属性项目
此 API 允许您通过编号检索产品的属性项目。
GET /wp-json/wc/v3/products/attributes/<attribute_id>/terms/<id>
- cURL
- JavaScript
- PHP
- Python
- Ruby
- JSON Response
curl https://example.com/wp-json/wc/v3/products/attributes/2/terms/23 \
-u consumer_key:consumer_secret
WooCommerce.get( 'products/attributes/2/terms/23' )
.then( ( response ) => {
console.log( response.data );
} )
.catch( ( error ) => {
console.log( error.response.data );
} );
<?php print_r($woocommerce->get('products/attributes/2/terms/23')); ?>
print(wcapi.get("products/attributes/2/terms/23").json())
woocommerce.get("products/attributes/2/terms/23").parsed_response
{
"id": 23,
"name": "XXS",
"slug": "xxs",
"description": "",
"menu_order": 1,
"count": 1,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms/23"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms"
}
]
}
}
列出所有属性条款
此 API 允许您从产品属性中检索所有条款。
GET /wp-json/wc/v3/products/attributes/<attribute_id>/terms
- cURL
- JavaScript
- PHP
- Python
- Ruby
- JSON Response
curl https://example.com/wp-json/wc/v3/products/attributes/2/terms \
-u consumer_key:consumer_secret
WooCommerce.get( 'products/attributes/2/terms' )
.then( ( response ) => {
console.log( response.data );
} )
.catch( ( error ) => {
console.log( error.response.data );
} );
<?php print_r($woocommerce->get('products/attributes/2/terms')); ?>
print(wcapi.get("products/attributes/2/terms").json())
woocommerce.get("products/attributes/2/terms").parsed_response
[
{
"id": 23,
"name": "XXS",
"slug": "xxs",
"description": "",
"menu_order": 1,
"count": 1,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms/23"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms"
}
]
}
},
{
"id": 22,
"name": "XS",
"slug": "xs",
"description": "",
"menu_order": 2,
"count": 1,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms/22"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms"
}
]
}
},
{
"id": 17,
"name": "S",
"slug": "s",
"description": "",
"menu_order": 3,
"count": 1,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms/17"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms"
}
]
}
},
{
"id": 18,
"name": "M",
"slug": "m",
"description": "",
"menu_order": 4,
"count": 1,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms/18"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms"
}
]
}
},
{
"id": 19,
"name": "L",
"slug": "l",
"description": "",
"menu_order": 5,
"count": 1,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms/19"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms"
}
]
}
},
{
"id": 20,
"name": "XL",
"slug": "xl",
"description": "",
"menu_order": 6,
"count": 1,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms/20"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms"
}
]
}
},
{
"id": 21,
"name": "XXL",
"slug": "xxl",
"description": "",
"menu_order": 7,
"count": 1,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms/21"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms"
}
]
}
}
]
Available parameters
| Parameter | Type | Description |
|---|---|---|
context | string | Scope under which the request is made; determines fields present in response. Options: view and edit. Default is view. |
page | integer | Current page of the collection. Default is 1. |
per_page | integer | Maximum number of items to be returned in result set. Default is 10. |
search | string | Limit results to those matching a string. |
exclude | array | Ensure result set excludes specific ids. |
include | array | Limit result set to specific ids. |
order | string | Order sort attribute ascending or descending. Options: asc and desc. Default is asc. |
orderby | string | Sort collection by resource attribute. Options: id, include, name, slug, term_group, description and count. Default is name. |
hide_empty | boolean | Whether to hide resources not assigned to any products. Default is false. |
parent | integer | Limit result set to resources assigned to a specific parent. |
product | integer | Limit result set to resources assigned to a specific product. |
slug | string | Limit result set to resources with a specific slug. |
Update an attribute term
This API lets you make changes to a product attribute term.
PUT /wp-json/wc/v3/products/attributes/<attribute_id>/terms/<id>
- cURL
- JavaScript
- PHP
- Python
- Ruby
- JSON Response
curl -X PUT https://example.com/wp-json/wc/v3/products/attributes/2/terms/23 \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"name": "XXS"
}'
const data = {
name: 'XXS',
};
WooCommerce.put( 'products/attributes/2/terms/23', data )
.then( ( response ) => {
console.log( response.data );
} )
.catch( ( error ) => {
console.log( error.response.data );
} );
<?php
$data = [
'name' => 'XXS'
];
print_r($woocommerce->put('products/attributes/2/terms/23', $data));
?>
data = {
"name": "XXS"
}
print(wcapi.put("products/attributes/2/terms/23", data).json())
data = {
name: "XXS"
}
woocommerce.put("products/attributes/2/terms/23", data).parsed_response
{
"id": 23,
"name": "XXS",
"slug": "xxs",
"description": "",
"menu_order": 1,
"count": 1,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms/23"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms"
}
]
}
}
删除一个属性项目
这个 API 可以帮助您删除一个产品属性项目。
DELETE /wp-json/wc/v3/products/attributes/<attribute_id>/terms/<id>
- cURL
- JavaScript
- PHP
- Python
- Ruby
- JSON Response
curl -X DELETE https://example.com/wp-json/wc/v3/products/attributes/2/terms/23?force=true \
-u consumer_key:consumer_secret
WooCommerce.delete( 'products/attributes/2/terms/23', {
force: true,
} )
.then( ( response ) => {
console.log( response.data );
} )
.catch( ( error ) => {
console.log( error.response.data );
} );
<?php print_r($woocommerce->delete('products/attributes/2/terms/23', ['force' => true])); ?>
print(wcapi.delete("products/attributes/2/terms/23", params={"force": True}).json())
woocommerce.delete("products/attributes/2/terms/23", force: true).parsed_response
{
"id": 23,
"name": "XXS",
"slug": "xxs",
"description": "",
"menu_order": 1,
"count": 1,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms/23"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms"
}
]
}
}
可用参数
| 参数 | 类型 | 描述 |
|---|---|---|
force | 字符串 | 必须为 true,因为资源不支持放入回收站。 |
批量更新属性项
此 API 可以帮助您批量创建、更新和删除多个产品属性项。
备注
注意:默认情况下,限制为最多可以创建、更新或删除 100 个对象。
POST /wp-json/wc/v3/products/attributes/<attribute_id>/terms/batch
- cURL
- JavaScript
- PHP
- Python
- Ruby
- JSON Response
curl -X POST https://example.com/wp-json/wc/v3/products/attributes/<attribute_id>/terms/batch \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"create": [
{
"name": "XXS"
},
{
"name": "S"
}
],
"update": [
{
"id": 19,
"menu_order": 6
}
],
"delete": [
21,
20
]
}'
const data = {
create: [
{
name: 'XXS',
},
{
name: 'S',
},
],
update: [
{
id: 19,
menu_order: 6,
},
],
delete: [ 21, 20 ],
};
WooCommerce.post( 'products/attributes/2/terms/batch', data )
.then( ( response ) => {
console.log( response.data );
} )
.catch( ( error ) => {
console.log( error.response.data );
} );
<?php
$data = [
'create' => [
[
'name' => 'XXS'
],
[
'name' => 'S'
]
],
'update' => [
[
'id' => 19,
'menu_order' => 6
]
],
'delete' => [
21,
20
]
];
print_r($woocommerce->post('products/attributes/2/terms/batch', $data));
?>
data = {
"create": [
{
"name": "XXS"
},
{
"name": "S"
}
],
"update": [
{
"id": 19,
"menu_order": 6
}
],
"delete": [
21,
20
]
}
print(wcapi.post("products/attributes/2/terms/batch", data).json())
data = {
create: [
{
name: "XXS"
},
{
name: "S"
}
],
update: [
{
id: 19,
menu_order: 6
}
],
delete: [
21,
20
]
}
woocommerce.post("products/attributes/2/terms/batch", data).parsed_response
{
"create": [
{
"id": 23,
"name": "XXS",
"slug": "xxs",
"description": "",
"menu_order": 1,
"count": 0,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms/23"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms"
}
]
}
},
{
"id": 17,
"name": "S",
"slug": "s",
"description": "",
"menu_order": 3,
"count": 0,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms/17"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms"
}
]
}
}
],
"update": [
{
"id": 19,
"name": "L",
"slug": "l",
"description": "",
"menu_order": 5,
"count": 1,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms/19"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms"
}
]
}
}
],
"delete": [
{
"id": 21,
"name": "XXL",
"slug": "xxl",
"description": "",
"menu_order": 7,
"count": 1,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms/21"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms"
}
]
}
},
{
"id": 20,
"name": "XL",
"slug": "xl",
"description": "",
"menu_order": 6,
"count": 1,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms/20"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2/terms"
}
]
}
}
]
}