woocommerce-rest-api-docs/source/includes/wp-api-v2/_product-attribute-terms.md
2019-09-03 21:23:48 -03:00

17 KiB

Product attribute terms

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

Product attribute term properties

Attribute Type Description
id integer Unique identifier for the resource. read-only
name string Term name. mandatory
slug string An alphanumeric identifier for the resource unique to its type.
description string HTML description of the resource.
menu_order integer Menu order, used to custom sort the resource.
count integer Number of published products for the resource. read-only

Create an attribute term

This API helps you to create a new product attribute term.

HTTP request

POST
/wp-json/wc/v2/products/attributes/<attribute_id>/terms
curl -X POST https://example.com/wp-json/wc/v2/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

JSON response example:

{
  "id": 23,
  "name": "XXS",
  "slug": "xxs",
  "description": "",
  "menu_order": 1,
  "count": 1,
  "_links": {
    "self": [
      {
        "href": "https://example.com/wp-json/wc/v2/products/attributes/2/terms/23"
      }
    ],
    "collection": [
      {
        "href": "https://example.com/wp-json/wc/v2/products/attributes/2/terms"
      }
    ]
  }
}

Retrieve an attribute term

This API lets you retrieve a product attribute term by ID.

GET
/wp-json/wc/v2/products/attributes/<attribute_id>/terms/<id>
curl https://example.com/wp-json/wc/v2/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

JSON response example:

{
  "id": 23,
  "name": "XXS",
  "slug": "xxs",
  "description": "",
  "menu_order": 1,
  "count": 1,
  "_links": {
    "self": [
      {
        "href": "https://example.com/wp-json/wc/v2/products/attributes/2/terms/23"
      }
    ],
    "collection": [
      {
        "href": "https://example.com/wp-json/wc/v2/products/attributes/2/terms"
      }
    ]
  }
}

List all attribute terms

This API lets you retrieve all terms from a product attribute.

GET
/wp-json/wc/v2/products/attributes/<attribute_id>/terms
curl https://example.com/wp-json/wc/v2/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

JSON response example:

[
  {
    "id": 23,
    "name": "XXS",
    "slug": "xxs",
    "description": "",
    "menu_order": 1,
    "count": 1,
    "_links": {
      "self": [
        {
          "href": "https://example.com/wp-json/wc/v2/products/attributes/2/terms/23"
        }
      ],
      "collection": [
        {
          "href": "https://example.com/wp-json/wc/v2/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/v2/products/attributes/2/terms/22"
        }
      ],
      "collection": [
        {
          "href": "https://example.com/wp-json/wc/v2/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/v2/products/attributes/2/terms/17"
        }
      ],
      "collection": [
        {
          "href": "https://example.com/wp-json/wc/v2/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/v2/products/attributes/2/terms/18"
        }
      ],
      "collection": [
        {
          "href": "https://example.com/wp-json/wc/v2/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/v2/products/attributes/2/terms/19"
        }
      ],
      "collection": [
        {
          "href": "https://example.com/wp-json/wc/v2/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/v2/products/attributes/2/terms/20"
        }
      ],
      "collection": [
        {
          "href": "https://example.com/wp-json/wc/v2/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/v2/products/attributes/2/terms/21"
        }
      ],
      "collection": [
        {
          "href": "https://example.com/wp-json/wc/v2/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.

HTTP request

PUT
/wp-json/wc/v2/products/attributes/<attribute_id>/terms/<id>
curl -X PUT https://example.com/wp-json/wc/v2/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

JSON response example:

{
  "id": 23,
  "name": "XXS",
  "slug": "xxs",
  "description": "",
  "menu_order": 1,
  "count": 1,
  "_links": {
    "self": [
      {
        "href": "https://example.com/wp-json/wc/v2/products/attributes/2/terms/23"
      }
    ],
    "collection": [
      {
        "href": "https://example.com/wp-json/wc/v2/products/attributes/2/terms"
      }
    ]
  }
}

Delete an attribute term

This API helps you delete a product attribute term.

HTTP request

DELETE
/wp-json/wc/v2/products/attributes/<attribute_id>/terms/<id>
curl -X DELETE https://example.com/wp-json/wc/v2/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

JSON response example:

{
  "id": 23,
  "name": "XXS",
  "slug": "xxs",
  "description": "",
  "menu_order": 1,
  "count": 1,
  "_links": {
    "self": [
      {
        "href": "https://example.com/wp-json/wc/v2/products/attributes/2/terms/23"
      }
    ],
    "collection": [
      {
        "href": "https://example.com/wp-json/wc/v2/products/attributes/2/terms"
      }
    ]
  }
}

Available parameters

Parameter Type Description
force string Required to be true, as resource does not support trashing.

Batch update attribute terms

This API helps you to batch create, update and delete multiple product attribute terms.

HTTP request

POST
/wp-json/wc/v2/products/attributes/<attribute_id>/terms/batch
curl -X POST https://example.com//wp-json/wc/v2/products/attributes/&lt;attribute_id&gt;/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

JSON response example:

{
  "create": [
    {
      "id": 23,
      "name": "XXS",
      "slug": "xxs",
      "description": "",
      "menu_order": 1,
      "count": 0,
      "_links": {
        "self": [
          {
            "href": "https://example.com/wp-json/wc/v2/products/attributes/2/terms/23"
          }
        ],
        "collection": [
          {
            "href": "https://example.com/wp-json/wc/v2/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/v2/products/attributes/2/terms/17"
          }
        ],
        "collection": [
          {
            "href": "https://example.com/wp-json/wc/v2/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/v2/products/attributes/2/terms/19"
          }
        ],
        "collection": [
          {
            "href": "https://example.com/wp-json/wc/v2/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/v2/products/attributes/2/terms/21"
          }
        ],
        "collection": [
          {
            "href": "https://example.com/wp-json/wc/v2/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/v2/products/attributes/2/terms/20"
          }
        ],
        "collection": [
          {
            "href": "https://example.com/wp-json/wc/v2/products/attributes/2/terms"
          }
        ]
      }
    }
  ]
}