跳转到主要内容

产品类别 API

本 API 允许您访问和管理产品类别信息。

目录

  • 获取所有产品类别
  • 按 ID 获取产品类别
  • 创建产品类别
  • 更新产品类别
  • 删除产品类别

获取所有产品类别

此 API 端点允许您获取所有可用的产品类别。

请求方法: GET

请求 URL: /api/v1/product_categories

响应:

[
  {
    "id": 1,
    "name": "Electronics",
    "description": "Electronic devices and accessories"
  },
  {
    "id": 2,
    "name": "Clothing",
    "description": "Apparel and fashion items"
  }
]

按 ID 获取产品类别

此 API 端点允许您通过 ID 获取特定产品类别的信息。

请求方法: GET

请求 URL: /api/v1/product_categories/{id}

参数:

  • id: 要获取的产品类别的 ID。

响应:

{
  "id": 1,
  "name": "Electronics",
  "description": "Electronic devices and accessories"
}

创建产品类别

此 API 端点允许您创建新的产品类别。

请求方法: POST

请求 URL: /api/v1/product_categories

请求体:

{
  "name": "New Category",
  "description": "Description of the new category"
}

响应:

{
  "id": 3,
  "name": "New Category",
  "description": "Description of the new category"
}

更新产品类别

此 API 端点允许您更新现有产品类别的属性。

请求方法: PUT

请求 URL: /api/v1/product_categories/{id}

参数:

  • id: 要更新的产品类别的 ID。

请求体:

{
  "name": "Updated Category Name",
  "description": "Updated description of the category"
}

响应:

{
  "id": 1,
  "name": "Updated Category Name",
  "description": "Updated description of the category"
}

删除产品类别

此 API 端点允许您删除现有产品类别。

请求方法: DELETE

请求 URL: /api/v1/product_categories/{id}

参数:

  • id: 要删除的产品类别的 ID。

响应:

成功删除时,返回状态码 204 No Content。

错误处理:

如果发生错误,API 将返回相应的错误代码和消息。例如,如果尝试访问不存在的产品类别,则返回 404 Not Found。

示例:

使用 curl 命令获取所有产品类别:

curl -X GET http://example.com/api/v1/product_categories

使用 curl 命令创建新的产品类别:

curl -X POST -H "Content-Type: application/json" -d '{"name": "New Category", "description": "Description of the new category"}' http://example.com/api/v1/product_categories

注意:

  • 所有 API 请求都需要有效的 API 密钥。
  • Pro 版本提供额外的功能和更高的速率限制。
  • Category 字段表示产品所属的分类。
  • Or 表示“或”。
  • at 表示“在”。
  • Go 表示“提交”。
  • 请参考 API 文档以获取更多详细信息。
  • 有关 API 使用的更多信息,请访问 API Documentation.
  • 您可以在 %s 处找到更多信息。
  • 请使用 %d 作为 ID。
  • 您的姓名是 {name}。

列出产品分类

GET /products/categories
属性 类型 必需 描述
context 字符串 否 发送请求的范围;决定响应中包含的字段。
page 整数 否 集合的当前页。默认为 1。
per_page 整数 否 结果集中返回的最大项目数。默认为无限制。允许的值在 0 和 100 之间。
search 字符串 否 限制结果,仅包含与字符串匹配的结果。
exclude 数组 否 确保结果集排除特定的 ID。
include 数组 否 限制结果集为特定的 ID。
order 字符串 否 升序或降序排序。允许的值:asc, desc。默认为 asc。
orderby 字符串 否 按属性排序。允许的值:name, slug, count。默认为 name。
hide_empty 布尔值 否 如果为 true,则不返回空项。默认为 true。
parent 整数 否 限制结果,仅包含具有特定父级 ID 的项。
curl "https://example-store.com/wp-json/wc/store/v1/products/categories"

示例响应:

[
	{
		"id": 16,
		"name": "Clothing",
		"slug": "clothing",
		"description": "This is the clothing category.",
		"parent": 0,
		"count": 11,
		"image": {
			"id": 55,
			"src": "https://store.local/wp-content/uploads/2021/11/t-shirt-with-logo-1.jpg",
			"thumbnail": "https://store.local/wp-content/uploads/2021/11/t-shirt-with-logo-1-324x324.jpg",
			"srcset": "https://store.local/wp-content/uploads/2021/11/t-shirt-with-logo-1.jpg 800w, https://store.local/wp-content/uploads/2021/11/t-shirt-with-logo-1-324x324.jpg 324w, https://store.local/wp-content/uploads/2021/11/t-shirt-with-logo-1-100x100.jpg 100w, https://store.local/wp-content/uploads/2021/11/t-shirt-with-logo-1-416x416.jpg 416w, https://store.local/wp-content/uploads/2021/11/t-shirt-with-logo-1-300x300.jpg 300w, https://store.local/wp-content/uploads/2021/11/t-shirt-with-logo-1-150x150.jpg 150w, https://store.local/wp-content/uploads/2021/11/t-shirt-with-logo-1-768x768.jpg 768w",
			"sizes": "(max-width: 800px) 100vw, 800px",
			"name": "t-shirt-with-logo-1.jpg",
			"alt": ""
		},
		"review_count": 2,
		"permalink": "https://store.local/product-category/clothing/"
	},
	{
		"id": 21,
		"name": "Decor",
		"slug": "decor",
		"description": "",
		"parent": 0,
		"count": 1,
		"image": null,
		"review_count": 1,
		"permalink": "https://store.local/product-category/decor/"
	}
]

产品分类

本页面展示了商店中的产品分类。

分类列表

以下是可用的分类列表:

分类详情

点击分类名称可以查看该分类下的所有产品。每个分类都有一个别名,用于URL链接。

分类属性

  • id: 分类ID。
  • name: 分类名称。
  • slug: 分类别名。
  • description: 分类描述。
  • parent: 父级分类ID。
  • count: 计数,表示该分类下的产品数量。
  • image: 分类图像信息。
    • id: 图像ID。
    • src: 图像原始链接。
    • thumbnail: 图像缩略图链接。
    • srcset: 图像不同尺寸的链接集合。
    • sizes: 图像尺寸属性。
    • name: 图像文件名。
    • alt: 图像替代文本。
  • review_count: 评价数量。
  • permalink: 固定链接。

上传

您可以在此处上传新的图像或文件。

日志

日志文件记录了系统操作。

商店设置

您可以在此处配置商店的各种设置。

宽度和尺寸

宽度和尺寸是图像的重要属性。

链接

链接到其他页面或资源。

内容

内容是网页的主要部分。

在线

在线状态。

本地

本地文件。

评论

评论是对产品或内容的反馈。

类别

分类用于组织内容。

别名

别名是名称的另一种形式,通常用于URL。

产品

产品是商店中的可售商品。

视图

查看页面或内容。

尺寸

尺寸是图像或产品的属性。

产品分类

获取单个分类。

GET /products/categories/:id
分类 类型 必填 描述
id 整数 是 要检索的分类的 ID。
curl "https://example-store.com/wp-json/wc/store/v1/products/categories/1"

示例回复:

{
	"id": 1,
	"name": "Decor",
	"slug": "decor",
	"description": "",
	"parent": 0,
	"count": 1,
	"image": null,
	"review_count": 1,
	"permalink": "https://store.local/product-category/decor/"
}