docs: update details on retrieving nested categories (#10317)

This commit is contained in:
Shahed Nasser
2024-11-27 18:52:33 +02:00
committed by GitHub
parent d9574f734e
commit 263dbf7872
6 changed files with 35 additions and 25 deletions
@@ -8,7 +8,11 @@ export const metadata = {
A product category has parent and child categories.
To retrieve the child or nested categories of a category in your storefront, pass `*category_children` to the `fields` query parameter of the [Get a Category API Route](!api!/store#product-categories_getproductcategoriesid).
To retrieve the child or nested categories of a category in your storefront, pass to the [Get a Category API Route](!api!/store#product-categories_getproductcategoriesid) the following query parameters:
- `include_descendants_tree=true` to retrieve each category's nested categories at all levels.
- Add `category_children` to `fields`, which is the field that will hold a category's nested categories. You can either pass `*category_children` to retrieve all fields of a child category, or specify the fields specifically to avoid a large response size. For example, `field=category_children.id,category_children.name`.
- `parent_category_id=null` to retrieve only the categories that don't have a parent. This avoids retrieving the child categories multiple times in the response since child categories are now set in their parent's object.
For example:
@@ -16,12 +20,16 @@ For example:
<CodeTab label="Fetch API" value="fetch">
export const fetchHighlights = [
["2", `"*category_children"`, "Select the `category_children` relation."],
["2", `"category_children.id,category_children.name"`, "Select the fields of category children"],
["3", `"include_descendants_tree"`, "Indicate that all nested categories should be retrieved."],
["4", "parent_category_id", "Since each category will have its children, you only retrieve categories that don't have a parent."]
]
```ts highlights={fetchHighlights}
const searchParams = new URLSearchParams({
fields: "*category_children",
fields: "category_children.id,category_children.name",
include_descendants_tree: true,
parent_category_id: null
})
fetch(`http://localhost:9000/store/product-categories/${id}?${
@@ -44,10 +52,11 @@ export const fetchHighlights = [
export const highlights = [
["12", "{ params: { id } }: Params", "This is based on Next.js which passes the path parameters as a prop."],
["24", `"*category_children"`, "Select the `category_children` relation."],
["27"], ["28"], ["29"],
["30"], ["31"], ["32"], ["33"], ["34"], ["35"], ["36"], ["37"], ["38"], ["39"],
["53", "", "Show the nested categories."],
["24", `"category_children.id,category_children.name"`, "Select the fields of category children"],
["25", "parent_category_id", "Since each category will have its children, you only retrieve categories that don't have a parent."]
["26"], ["27"], ["28"],
["29"], ["30"], ["31"], ["32"], ["33"], ["34"], ["35"], ["36"], ["37"], ["38"],
["54", "", "Show the nested categories."],
]
```tsx highlights={highlights}
@@ -74,7 +83,8 @@ export const highlights = [
}
const searchParams = new URLSearchParams({
fields: "*category_children",
fields: "category_children.id,category_children.name",
parent_category_id: null
})
fetch(`http://localhost:9000/store/product-categories/${id}?${