WooCommerce REST API 文档

title: "Product - Attributes #" post_status: publish comment_status: open taxonomy: category: - woocommerce-rest-api post_tag: - V3 - Includes - Source


Product - Attributes

This section lists all API endpoints that can be used to create, edit or otherwise manipulate product attributes.

Product Attribute Properties

Attribute Type Description
id integer Attribute ID read-only
name string Attribute name
slug string Attribute slug
type string Attribute type, the types available include by default are: select and text (some plugins can include new types)
order_by string Default sort order. Available: menu_order, name, name_num and id
has_archives boolean Enable/Disable attribute archives

创建产品属性

此 API 可帮助您创建新的产品属性。

HTTP 请求

POST
/wc-api/v3/products/attributes
curl -X POST https://example.com/wc-api/v3/products/attributes \
    -u consumer_key:consumer_secret \
    -H "Content-Type: application/json" \
    -d '{
  "product_attribute": {
    "name": "Color",
    "slug": "pa_color",
    "type": "select",
    "order_by": "menu_order",
    "has_archives": true
  }
}'
var data = {
  product_attribute: {
    name: 'Color',
    slug: 'pa_color',
    type: 'select',
    order_by: 'menu_order',
    has_archives: true
  }
};

WooCommerce.post('products/attributes', data, function(err, data, res) {
  console.log(res);
});
<?php
$data = [
    'product_attribute' => [
        'name' => 'Color',
        'slug' => 'pa_color',
        'type' => 'select',
        'order_by' => 'menu_order',
        'has_archives' => true
    ]
];

print_r($woocommerce->post('products/attributes', $data));
?>
data = {
    "product_attribute": {
        "name": "Color",
        "slug": "pa_color",
        "type": "select",
        "order_by": "menu_order",
        "has_archives": True
    }
}

print(wcapi.post("products/attributes", data).json())
data = {
  product_attribute: {
    name: "Color",
    slug: "pa_color",
    type: "select",
    order_by: "menu_order",
    has_archives: true
  }
}

woocommerce.post("products/attributes", data).parsed_response

JSON 响应示例:

{
  "product_attribute": {
    "id": 1,
    "name": "Color",
    "slug": "pa_color",
    "type": "select",
    "order_by": "menu_order",
    "has_archives": true
  }
}

查看产品属性

此 API 允许您通过 ID 检索并查看特定的产品属性。

GET
/wc-api/v3/products/attributes/<id>
curl https://example.com/wc-api/v3/products/attributes/1 \
    -u consumer_key:consumer_secret
WooCommerce.get('products/attributes/1', function(err, data, res) {
  console.log(res);
});
<?php print_r($woocommerce->get('products/attributes/1')); ?>
print(wcapi.get("products/attributes/1").json())
woocommerce.get("products/attributes/1").parsed_response

JSON 响应示例:

{
  "product_attribute": {
    "id": 1,
    "name": "Color",
    "slug": "pa_color",
    "type": "select",
    "order_by": "menu_order",
    "has_archives": true
  }
}

查看产品属性列表

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

HTTP 请求

GET
/wc-api/v3/products/attributes
curl https://example.com/wc-api/v3/products/attributes \
    -u consumer_key:consumer_secret
WooCommerce.get('products/attributes', function(err, data, res) {
  console.log(res);
});
<?php print_r($woocommerce->get('products/attributes')); ?>
print(wcapi.get("products/attributes").json())
woocommerce.get("products/attributes").parsed_response

JSON 响应示例:

{
  "product_attributes": [
    {
      "id": 1,
      "name": "Color",
      "slug": "pa_color",
      "type": "select",
      "order_by": "menu_order",
      "has_archives": true
    },
    {
      "id": 2,
      "name": "Size",
      "slug": "pa_size",
      "type": "select",
      "order_by": "menu_order",
      "has_archives": false
    }
  ]
}

更新产品属性

此 API 允许您修改产品属性。

HTTP 请求

PUT
/wc-api/v3/products/attributes/<id>
curl -X PUT https://example.com/wc-api/v3/products/attributes/1 \
    -u consumer_key:consumer_secret \
    -H "Content-Type: application/json" \
    -d '{
  "product_attribute": {
    "order_by": "name"
  }
}'
var data = {
  product_attribute: {
    order_by: 'name'
  }
};

WooCommerce.put('products/attributes/1', data, function(err, data, res) {
  console.log(res);
});
<?php
$data = [
    'product_attribute' => [
        'order_by' => 'name'
    ]
];

print_r($woocommerce->put('products/attributes/1', $data));
?>
data = {
    "product_attribute": {
        "order_by": "name"
    }
}

print(wcapi.put("products/attributes/1", data).json())
data = {
  product_attribute: {
    order_by: "name"
  }
}

woocommerce.put("products/attributes/1", data).parsed_response

JSON 响应示例:

{
  "product_attribute": {
    "id": 1,
    "name": "Color",
    "slug": "pa_color",
    "type": "select",
    "order_by": "name",
    "has_archives": true
  }
}

删除产品属性

此 API 可帮助您删除一个产品属性。

HTTP 请求

DELETE
/wc-api/v3/products/attributes/<id>
curl -X DELETE https://example.com/wc-api/v3/products/attributes/1 \
    -u consumer_key:consumer_secret
WooCommerce.delete('products/attributes/1', function(err, data, res) {
  console.log(res);
});
<?php print_r($woocommerce->delete('products/attributes/1')); ?>
print(wcapi.delete("products/attributes/1").json())
woocommerce.delete("products/attributes/1").parsed_response

JSON 响应示例:

{
  "message": "Deleted product_attribute"
}