将当前页面内容以 Markdown 格式复制到剪贴板
产品属性
产品属性 API 允许您创建、查看、更新和删除单个或批量产品属性。
产品属性的属性
| 属性 | 类型 | 描述 |
|---|---|---|
id | integer | 该资源的唯一标识符。只读 |
name | string | 属性名称。必填 |
slug | string | 用于该资源类型的字母数字标识符。 |
type | string | 属性类型。默认情况下仅支持 select。 |
order_by | string | 默认排序方式。选项:menu_order, name, name_num 和 id。默认为 menu_order。 |
has_archives | boolean | 启用/禁用属性归档。默认为 false。 |
创建一个产品属性
此 API 可帮助您创建新的产品属性。
POST /wp-json/wc/v3/products/attributes
- cURL
- JavaScript
- PHP
- Python
- Ruby
- JSON Response
curl -X POST https://example.com/wp-json/wc/v3/products/attributes \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"name": "Color",
"slug": "pa_color",
"type": "select",
"order_by": "menu_order",
"has_archives": true
}'
const data = {
name: 'Color',
slug: 'pa_color',
type: 'select',
order_by: 'menu_order',
has_archives: true,
};
WooCommerce.post( 'products/attributes', data )
.then( ( response ) => {
console.log( response.data );
} )
.catch( ( error ) => {
console.log( error.response.data );
} );
<?php
$data = [
'name' => 'Color',
'slug' => 'pa_color',
'type' => 'select',
'order_by' => 'menu_order',
'has_archives' => true
];
print_r($woocommerce->post('products/attributes', $data));
?>
data = {
"name": "Color",
"slug": "pa_color",
"type": "select",
"order_by": "menu_order",
"has_archives": True
}
print(wcapi.post("products/attributes", data).json())
data = {
name: "Color",
slug: "pa_color",
type: "select",
order_by: "menu_order",
has_archives: true
}
woocommerce.post("products/attributes", data).parsed_response
{
"id": 1,
"name": "Color",
"slug": "pa_color",
"type": "select",
"order_by": "menu_order",
"has_archives": true,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/6"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes"
}
]
}
}
获取产品属性
此 API 允许您通过编号检索并查看特定产品的属性。
GET /wp-json/wc/v3/products/attributes/<id>
- cURL
- JavaScript
- PHP
- Python
- Ruby
- JSON Response
curl https://example.com/wp-json/wc/v3/products/attributes/1 \
-u consumer_key:consumer_secret
WooCommerce.get( 'products/attributes/1' )
.then( ( response ) => {
console.log( response.data );
} )
.catch( ( error ) => {
console.log( error.response.data );
} );
<?php print_r($woocommerce->get('products/attributes/1')); ?>
print(wcapi.get("products/attributes/1").json())
woocommerce.get("products/attributes/1").parsed_response
{
"id": 1,
"name": "Color",
"slug": "pa_color",
"type": "select",
"order_by": "menu_order",
"has_archives": true,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/6"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes"
}
]
}
}
列出所有产品属性
此 API 帮助您查看所有产品属性。
GET /wp-json/wc/v3/products/attributes
- cURL
- JavaScript
- PHP
- Python
- Ruby
- JSON Response
curl https://example.com/wp-json/wc/v3/products/attributes \
-u consumer_key:consumer_secret
WooCommerce.get( 'products/attributes' )
.then( ( response ) => {
console.log( response.data );
} )
.catch( ( error ) => {
console.log( error.response.data );
} );
<?php print_r($woocommerce->get('products/attributes')); ?>
print(wcapi.get("products/attributes").json())
woocommerce.get("products/attributes").parsed_response
[
{
"id": 1,
"name": "Color",
"slug": "pa_color",
"type": "select",
"order_by": "menu_order",
"has_archives": true,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/6"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes"
}
]
}
},
{
"id": 2,
"name": "Size",
"slug": "pa_size",
"type": "select",
"order_by": "menu_order",
"has_archives": false,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes"
}
]
}
}
]
可用参数
| 参数 | 类型 | 描述 |
|---|---|---|
context | 字符串 | 发送请求的上下文;决定了回复中包含的字段。选项:view 和 edit。默认值为 view。 |
更新产品属性
此 API 允许您修改产品属性。
PUT /wp-json/wc/v3/products/attributes/<id>
- cURL
- JavaScript
- PHP
- Python
- Ruby
- JSON Response
curl -X PUT https://example.com/wp-json/wc/v3/products/attributes/1 \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"order_by": "name"
}'
const data = {
order_by: 'name',
};
WooCommerce.put( 'products/attributes/1', data )
.then( ( response ) => {
console.log( response.data );
} )
.catch( ( error ) => {
console.log( error.response.data );
} );
<?php
$data = [
'order_by' => 'name'
];
print_r($woocommerce->put('products/attributes/1', $data));
?>
data = {
"order_by": "name"
}
print(wcapi.put("products/attributes/1", data).json())
data = {
order_by: "name"
}
woocommerce.put("products/attributes/1", data).parsed_response
{
"id": 1,
"name": "Color",
"slug": "pa_color",
"type": "select",
"order_by": "name",
"has_archives": true,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/6"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes"
}
]
}
}
删除产品属性
此 API 可以帮助您删除一个产品属性。
DELETE /wp-json/wc/v3/products/attributes/<id>
注意
这也会删除所有已选中的属性的条款。
- cURL
- JavaScript
- PHP
- Python
- Ruby
- JSON Response
curl -X DELETE https://example.com/wp-json/wc/v3/products/attributes/1?force=true \
-u consumer_key:consumer_secret
WooCommerce.delete( 'products/attributes/1', {
force: true,
} )
.then( ( response ) => {
console.log( response.data );
} )
.catch( ( error ) => {
console.log( error.response.data );
} );
<?php print_r($woocommerce->delete('products/attributes/1', ['force' => true])); ?>
print(wcapi.delete("products/attributes/1", params={"force": True}).json())
woocommerce.delete("products/attributes/1", force: true).parsed_response
{
"id": 1,
"name": "Color",
"slug": "pa_color",
"type": "select",
"order_by": "menu_order",
"has_archives": true,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/6"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes"
}
]
}
}
可用参数
| 参数 | 类型 | 描述 |
|---|---|---|
force | 字符串 | 必须为 true,因为资源不支持放入回收站。 |
批量更新产品属性
此 API 可以帮助您批量创建、更新和删除多个产品属性。
备注
注意:默认情况下,限制为最多可以创建、更新或删除 100 个对象。
POST /wp-json/wc/v3/products/attributes/batch
- cURL
- JavaScript
- PHP
- Python
- Ruby
- JSON Response
curl -X POST https://example.com/wp-json/wc/v3/products/attributes/batch \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"create": [
{
"name": "Brand"
},
{
"name": "Publisher"
}
],
"update": [
{
"id": 2,
"order_by": "name"
}
],
"delete": [
1
]
}'
const data = {
create: [
{
name: 'Brand',
},
{
name: 'Publisher',
},
],
update: [
{
id: 2,
order_by: 'name',
},
],
delete: [ 1 ],
};
WooCommerce.post( 'products/attributes/batch', data )
.then( ( response ) => {
console.log( response.data );
} )
.catch( ( error ) => {
console.log( error.response.data );
} );
<?php
$data = [
'create' => [
[
'name' => 'Brand'
],
[
'name' => 'Publisher'
]
],
'update' => [
[
'id' => 2,
'order_by' => 'name'
]
],
'delete' => [
1
]
];
print_r($woocommerce->post('products/attributes/batch', $data));
?>
data = {
"create": [
{
"name": "Brand"
},
{
"name": "Publisher"
}
],
"update": [
{
"id": 2,
"order_by": "name"
}
],
"delete": [
1
]
}
print(wcapi.post("products/attributes/batch", data).json())
data = {
create: [
{
name: "Round toe"
},
{
name: "Flat"
}
],
update: [
{
id: 2,
order_by: "name"
}
],
delete: [
1
]
}
woocommerce.post("products/attributes/batch", data).parsed_response
{
"create": [
{
"id": 7,
"name": "Brand",
"slug": "pa_brand",
"type": "select",
"order_by": "menu_order",
"has_archives": false,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/7"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes"
}
]
}
},
{
"id": 8,
"name": "Publisher",
"slug": "pa_publisher",
"type": "select",
"order_by": "menu_order",
"has_archives": false,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/8"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes"
}
]
}
}
],
"update": [
{
"id": 2,
"name": "Size",
"slug": "pa_size",
"type": "select",
"order_by": "menu_order",
"has_archives": false,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/2"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes"
}
]
}
}
],
"delete": [
{
"id": 1,
"name": "Color",
"slug": "pa_color",
"type": "select",
"order_by": "menu_order",
"has_archives": true,
"_links": {
"self": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes/6"
}
],
"collection": [
{
"href": "https://example.com/wp-json/wc/v3/products/attributes"
}
]
}
}
]
}