title: "Product categories #" post_status: publish comment_status: open taxonomy: category: - woocommerce-rest-api post_tag: - Wp Api V1 - Includes - Source


Product categories

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

Product category properties

Attribute Type Description
id integer Unique identifier for the resource. read-only
name string Category name. required
slug string An alphanumeric identifier for the resource unique to its type.
parent integer The id for the parent of the resource.
description string HTML description of the resource.
display string Category archive display type. Default is default. Options: default, products, subcategories and both
image array Image data. See Category Image properties
menu_order integer Menu order, used to custom sort the resource.
count integer Number of published products for the resource. read-only

Category Image properties

Attribute Type Description
id integer Image ID (attachment ID). In write-mode used to attach pre-existing images.
date_created date-time The date the image was created, in the site's timezone. read-only
date_modified date-time The date the image was last modified, in the site's timezone. read-only
src string Image URL. In write-mode used to upload new images.
title string Image name.
alt string Image alternative text.

创建产品分类

此 API 可帮助您创建新的产品分类。

HTTP 请求

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

如何创建产品分类的示例:

curl -X POST https://example.com/wp-json/wc/v1/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"
  }
}'
const data = {
  name: "Clothing",
  image: {
    src: "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg"
  }
};

WooCommerce.post("products/categories", data)
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.log(error.response.data);
  });
<?php
$data = [
    'name' => 'Clothing',
    'image' => [
        'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg'
    ]
];

print_r($woocommerce->post('products/categories', $data));
?>
data = {
    "name": "Clothing",
    "image": {
        "src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg"
    }
}

print(wcapi.post("products/categories", data).json())
data = {
  name: "Clothing",
  image: {
    src: "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg"
  }
}

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

JSON 响应示例:

{
  "id": 9,
  "name": "Clothing",
  "slug": "clothing",
  "parent": 0,
  "description": "",
  "display": "default",
  "image": {
    "id": 173,
    "date_created": "2016-05-31T23:51:03",
    "date_modified": "2016-05-31T23:51:03",
    "src": "https://example/wp-content/uploads/2016/05/T_3_front-1.jpg",
    "title": "",
    "alt": ""
  },
  "menu_order": 0,
  "count": 18,
  "_links": {
    "self": [
      {
        "href": "https://example/wp-json/wc/v1/products/categories/9"
      }
    ],
    "collection": [
      {
        "href": "https://example/wp-json/wc/v1/products/categories"
      }
    ]
  }
}

获取商品分类

此 API 允许您通过 ID 检索商品分类。

GET
/wp-json/wc/v1/products/categories/<id>
curl https://example.com/wp-json/wc/v1/products/categories/9 \
    -u consumer_key:consumer_secret
WooCommerce.get("products/categories/9")
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.log(error.response.data);
  });
<?php print_r($woocommerce->get('products/categories/9')); ?>
print(wcapi.get("products/categories/9").json())
woocommerce.get("products/categories/9").parsed_response

JSON 响应示例:

{
  "id": 9,
  "name": "Clothing",
  "slug": "clothing",
  "parent": 0,
  "description": "",
  "display": "default",
  "image": {
    "id": 173,
    "date_created": "2016-05-31T23:51:03",
    "date_modified": "2016-05-31T23:51:03",
    "src": "https://example/wp-content/uploads/2016/05/T_3_front-1.jpg",
    "title": "",
    "alt": ""
  },
  "menu_order": 0,
  "count": 18,
  "_links": {
    "self": [
      {
        "href": "https://example/wp-json/wc/v1/products/categories/9"
      }
    ],
    "collection": [
      {
        "href": "https://example/wp-json/wc/v1/products/categories"
      }
    ]
  }
}

列出所有产品分类

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

GET
/wp-json/wc/v1/products/categories
curl https://example.com/wp-json/wc/v1/products/categories \
    -u consumer_key:consumer_secret
WooCommerce.get("products/categories")
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.log(error.response.data);
  });
<?php print_r($woocommerce->get('products/categories')); ?>
print(wcapi.get("products/categories").json())
woocommerce.get("products/categories").parsed_response

JSON 响应示例:

[
  {
    "id": 15,
    "name": "Albums",
    "slug": "albums",
    "parent": 11,
    "description": "",
    "display": "default",
    "image": [],
    "menu_order": 0,
    "count": 4,
    "_links": {
      "self": [
        {
          "href": "https://example.com/wp-json/wc/v1/products/categories/15"
        }
      ],
      "collection": [
        {
          "href": "https://example.com/wp-json/wc/v1/products/categories"
        }
      ],
      "up": [
        {
          "href": "https://example.com/wp-json/wc/v1/products/categories/11"
        }
      ]
    }
  },
  {
    "id": 9,
    "name": "Clothing",
    "slug": "clothing",
    "parent": 0,
    "description": "",
    "display": "default",
    "image": {
      "id": 173,
      "date_created": "2016-05-31T23:51:03",
      "date_modified": "2016-05-31T23:51:03",
      "src": "https://example/wp-content/uploads/2016/05/T_3_front-1.jpg",
      "title": "",
      "alt": ""
    },
    "menu_order": 0,
    "count": 18,
    "_links": {
      "self": [
        {
          "href": "https://example/wp-json/wc/v1/products/categories/9"
        }
      ],
      "collection": [
        {
          "href": "https://example/wp-json/wc/v1/products/categories"
        }
      ]
    }
  },
  {
    "id": 10,
    "name": "Hoodies",
    "slug": "hoodies",
    "parent": 9,
    "description": "",
    "display": "default",
    "image": [],
    "menu_order": 0,
    "count": 6,
    "_links": {
      "self": [
        {
          "href": "https://example.com/wp-json/wc/v1/products/categories/10"
        }
      ],
      "collection": [
        {
          "href": "https://example.com/wp-json/wc/v1/products/categories"
        }
      ],
      "up": [
        {
          "href": "https://example.com/wp-json/wc/v1/products/categories/9"
        }
      ]
    }
  },
  {
    "id": 11,
    "name": "Music",
    "slug": "music",
    "parent": 0,
    "description": "",
    "display": "default",
    "image": [],
    "menu_order": 0,
    "count": 7,
    "_links": {
      "self": [
        {
          "href": "https://example.com/wp-json/wc/v1/products/categories/11"
        }
      ],
      "collection": [
        {
          "href": "https://example.com/wp-json/wc/v1/products/categories"
        }
      ]
    }
  },
  {
    "id": 12,
    "name": "Posters",
    "slug": "posters",
    "parent": 0,
    "description": "",
    "display": "default",
    "image": [],
    "menu_order": 0,
    "count": 5,
    "_links": {
      "self": [
        {
          "href": "https://example.com/wp-json/wc/v1/products/categories/12"
        }
      ],
      "collection": [
        {
          "href": "https://example.com/wp-json/wc/v1/products/categories"
        }
      ]
    }
  },
  {
    "id": 13,
    "name": "Singles",
    "slug": "singles",
    "parent": 11,
    "description": "",
    "display": "default",
    "image": [],
    "menu_order": 0,
    "count": 3,
    "_links": {
      "self": [
        {
          "href": "https://example.com/wp-json/wc/v1/products/categories/13"
        }
      ],
      "collection": [
        {
          "href": "https://example.com/wp-json/wc/v1/products/categories"
        }
      ],
      "up": [
        {
          "href": "https://example.com/wp-json/wc/v1/products/categories/11"
        }
      ]
    }
  },
  {
    "id": 14,
    "name": "T-shirts",
    "slug": "t-shirts",
    "parent": 9,
    "description": "",
    "display": "default",
    "image": [],
    "menu_order": 0,
    "count": 6,
    "_links": {
      "self": [
        {
          "href": "https://example.com/wp-json/wc/v1/products/categories/14"
        }
      ],
      "collection": [
        {
          "href": "https://example.com/wp-json/wc/v1/products/categories"
        }
      ],
      "up": [
        {
          "href": "https://example.com/wp-json/wc/v1/products/categories/9"
        }
      ]
    }
  }
]

Available parameters

Parameter Type Description
context string Scope under which the request is made; determines fields present in response. Options: view and edit.
page integer Current page of the collection.
per_page integer Maximum number of items to be returned in result set.
search string Limit results to those matching a string.
exclude string Ensure result set excludes specific ids.
include string Limit result set to specific ids.
order string Order sort attribute ascending or descending. Default is asc. Options: asc and desc.
orderby string Sort collection by object attribute. Default is name. Options: id, include, name, slug, term_group, description and count.
hide_empty bool 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.

更新产品分类

此 API 允许您修改产品分类。

HTTP 请求

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

WooCommerce.put("products/categories/9", data)
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.log(error.response.data);
  });
<?php
$data = [
    'description' => 'All kinds of clothes.'
];

print_r($woocommerce->put('products/categories/9', $data));
?>
data = {
    "description": "All kinds of clothes."
}

print(wcapi.put("products/categories/9", data).json())
data = {
  description: "All kinds of clothes."
}

woocommerce.put("products/categories/9", data).parsed_response

JSON 响应示例:

{
  "id": 9,
  "name": "Clothing",
  "slug": "clothing",
  "parent": 0,
  "description": "All kinds of clothes.",
  "display": "default",
  "image": {
    "id": 173,
    "date_created": "2016-05-31T23:51:03",
    "date_modified": "2016-05-31T23:51:03",
    "src": "https://example/wp-content/uploads/2016/05/T_3_front-1.jpg",
    "title": "",
    "alt": ""
  },
  "menu_order": 0,
  "count": 18,
  "_links": {
    "self": [
      {
        "href": "https://example/wp-json/wc/v1/products/categories/9"
      }
    ],
    "collection": [
      {
        "href": "https://example/wp-json/wc/v1/products/categories"
      }
    ]
  }
}

删除产品分类

此 API 可帮助您删除产品分类。

HTTP 请求

DELETE
/wp-json/wc/v1/products/categories/<id>
curl -X DELETE https://example.com/wp-json/wc/v1/products/categories/9?force=true \
    -u consumer_key:consumer_secret
WooCommerce.delete("products/categories/9", {
  force: true
})
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.log(error.response.data);
  });
<?php print_r($woocommerce->delete('products/categories/9', ['force' => true])); ?>
print(wcapi.delete("products/categories/9", params={"force": True}).json())
woocommerce.delete("products/categories/9", force: true).parsed_response

JSON 响应示例:

{
  "id": 9,
  "name": "Clothing",
  "slug": "clothing",
  "parent": 0,
  "description": "All kinds of clothes.",
  "display": "default",
  "image": {
    "id": 173,
    "date_created": "2016-05-31T23:51:03",
    "date_modified": "2016-05-31T23:51:03",
    "src": "https://example/wp-content/uploads/2016/05/T_3_front-1.jpg",
    "title": "",
    "alt": ""
  },
  "menu_order": 0,
  "count": 18,
  "_links": {
    "self": [
      {
        "href": "https://example/wp-json/wc/v1/products/categories/9"
      }
    ],
    "collection": [
      {
        "href": "https://example/wp-json/wc/v1/products/categories"
      }
    ]
  }
}

Available parameters

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

批量更新产品分类

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

HTTP 请求

POST
/wp-json/wc/v1/products/categories/batch
curl -X POST https://example.com//wp-json/wc/v1/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
  ]
}'
const data = {
  create: [
    {
      name: "Albums"
    },
    {
      name: "Clothing"
    }
  ],
  update: [
    {
      id: 10,
      description: "Nice hoodies"
    }
  ],
  delete: [
    11,
    12
  ]
};

WooCommerce.post("products/categories/batch", data)
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.log(error.response.data);
  });
<?php
$data = [
    'create' => [
        [
            'name' => 'Albums'
        ],
        [
            'name' => 'Clothing'
        ]
    ],
    'update' => [
        [
            'id' => 10,
            'description' => 'Nice hoodies'
        ]
    ],
    'delete' => [
        11,
        12
    ]
];

print_r($woocommerce->post('products/categories/batch', $data));
?>
data = {
    "create": [
        {
            "name": "Albums"
        },
        {
            "name": "Clothing"
        }
    ],
    "update": [
        {
            "id": 10,
            "description": "Nice hoodies"
        }
    ],
    "delete": [
        11,
        12
    ]
}

print(wcapi.post("products/categories/batch", data).json())
data = {
  create: [
    {
      name: "Albums"
    },
    {
      name: "Clothing"
    }
  ],
  update: [
    {
      id: 10,
      description: "Nice hoodies"
    }
  ],
  delete: [
    11,
    12
  ]
}

woocommerce.post("products/categories/batch", data).parsed_response

JSON 响应示例:

{
  "create": [
    {
      "id": 15,
      "name": "Albums",
      "slug": "albums",
      "parent": 11,
      "description": "",
      "display": "default",
      "image": [],
      "menu_order": 0,
      "count": 0,
      "_links": {
        "self": [
          {
            "href": "https://example.com/wp-json/wc/v1/products/categories/15"
          }
        ],
        "collection": [
          {
            "href": "https://example.com/wp-json/wc/v1/products/categories"
          }
        ],
        "up": [
          {
            "href": "https://example.com/wp-json/wc/v1/products/categories/11"
          }
        ]
      }
    },
    {
      "id": 9,
      "name": "Clothing",
      "slug": "clothing",
      "parent": 0,
      "description": "",
      "display": "default",
      "image": [],
      "menu_order": 0,
      "count": 0,
      "_links": {
        "self": [
          {
            "href": "https://example.com/wp-json/wc/v1/products/categories/9"
          }
        ],
        "collection": [
          {
            "href": "https://example.com/wp-json/wc/v1/products/categories"
          }
        ]
      }
    }
  ],
  "update": [
    {
      "id": 10,
      "name": "Hoodies",
      "slug": "hoodies",
      "parent": 9,
      "description": "Nice hoodies",
      "display": "default",
      "image": [],
      "menu_order": 0,
      "count": 6,
      "_links": {
        "self": [
          {
            "href": "https://example.com/wp-json/wc/v1/products/categories/10"
          }
        ],
        "collection": [
          {
            "href": "https://example.com/wp-json/wc/v1/products/categories"
          }
        ],
        "up": [
          {
            "href": "https://example.com/wp-json/wc/v1/products/categories/9"
          }
        ]
      }
    }
  ],
  "delete": [
    {
      "id": 11,
      "name": "Music",
      "slug": "music",
      "parent": 0,
      "description": "",
      "display": "default",
      "image": [],
      "menu_order": 0,
      "count": 7,
      "_links": {
        "self": [
          {
            "href": "https://example.com/wp-json/wc/v1/products/categories/11"
          }
        ],
        "collection": [
          {
            "href": "https://example.com/wp-json/wc/v1/products/categories"
          }
        ]
      }
    },
    {
      "id": 12,
      "name": "Posters",
      "slug": "posters",
      "parent": 0,
      "description": "",
      "display": "default",
      "image": [],
      "menu_order": 0,
      "count": 5,
      "_links": {
        "self": [
          {
            "href": "https://example.com/wp-json/wc/v1/products/categories/12"
          }
        ],
        "collection": [
          {
            "href": "https://example.com/wp-json/wc/v1/products/categories"
          }
        ]
      }
    }
  ]
}