k.commerce.category
Product category management.
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
KScript API (k.api.get / k.api.post), server-side <script env="server"> | E-commerce module enabled | Query category trees and breadcrumb paths, maintain localized fields, and manage category-product membership |
k.commerce.category provides CRUD, ancestor-chain lookup, single/batch multilingual field updates, and product assignment operations for product categories.
Quick Start
k.api.get(() => {
const categories = k.commerce.category.list()
if (categories.length === 0) {
return { count: 0 }
}
const first = k.commerce.category.get(categories[0].id)
return {
count: categories.length,
firstTitle: first ? first.title : null,
childrenCount: first ? first.children.length : 0
}
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
list(options?) | List categories with optional offline inclusion and where filter | CategorySimple[] |
get(seoNameOrId) | Get category details including ancestor path and child categories | CategoryDetail | null |
getParents(seoNameOrId) | Get the parent category chain from immediate parent up to root (or null when blank) | CategorySimple[] | null |
create(value) | Create a new category | Category |
updateField(id, property, value, lang?) | Update a single field (optionally for a specific language) | void |
updateFields(id, updateFields) | Batch update multiple fields (optionally per language) | void |
addProduct(id, productId) | Assign a product to the category | void |
removeProduct(id, productId) | Remove a product from the category | void |
delete(id) | Delete a category | void |
list()
Get a list of categories.
| Parameter | Type | Required | Description |
|---|---|---|---|
options | CategoryQueryOptions | No | Category list filters |
options.includeOffline | boolean | No | Whether to include offline categories (default false) |
options.where | any | No | Filter condition expression |
Returns: CategorySimple[]. Category list matching the filters.
k.api.get(() => {
const categories = k.commerce.category.list()
return { count: categories.length, categories: categories.slice(0, 3) }
})
// Filter with parameters
k.api.get(() => {
const categories = k.commerce.category.list({ includeOffline: true })
return { count: categories.length }
})get()
Get category details.
Category Not Found
Returns null when no matching category is found.
| Parameter | Type | Required | Description |
|---|---|---|---|
seoNameOrId | string | Yes | Category SEO name or category ID |
Returns: CategoryDetail | null. Matching category details, or null when it does not exist.
k.api.get(() => {
const category = k.commerce.category.get('category-seo-name')
if (!category) {
return { error: 'Category not found' }
}
return { title: category.title, id: category.id }
})create()
Create a category.
| Parameter | Type | Required | Description |
|---|---|---|---|
value | NewCategory | Yes | New category data |
Returns: Category. Created category entity object.
k.api.post(() => {
const category = k.commerce.category.create({
title: 'Test Category',
description: 'Test Description',
seoName: 'test-category-' + Date.now(),
active: true
})
return { id: category.id, title: category.title }
})updateField()
Update a single category field.
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Category ID |
property | string | Yes | Field name |
value | any | Yes | Field value |
lang | string | No | Language code for multilingual fields |
Returns: void.
k.api.post(() => {
const category = k.commerce.category.create({
title: 'Field Update Test',
seoName: 'field-test-' + Date.now(),
active: true
})
k.commerce.category.updateField(category.id, 'title', 'Updated Title')
const updated = k.commerce.category.get(category.id)
k.commerce.category.delete(category.id)
return { id: category.id, title: updated.title }
})
// Multilingual update
k.api.post(() => {
const categoryId = 'category-id'
k.commerce.category.updateField(categoryId, 'title', 'New Title', 'en')
const updated = k.commerce.category.get(categoryId)
return { id: categoryId, title: updated?.title }
})updateFields()
Batch update multiple fields.
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Category ID |
updateFields | UpdateFieldParams[] | Yes | List of field updates |
updateFields[].property | string | Yes | Field name |
updateFields[].value | any | Yes | Field value |
updateFields[].lang | string | No | Language code for multilingual fields |
Returns: void.
k.api.post(() => {
const category = k.commerce.category.create({
title: 'Batch Update Test',
seoName: 'batch-test-' + Date.now(),
active: true
})
k.commerce.category.updateFields(category.id, [
{ property: 'title', value: 'Updated Title', lang: 'en' },
{ property: 'tags', value: ['tag1', 'tag2'] }
])
const updated = k.commerce.category.get(category.id)
k.commerce.category.delete(category.id)
return { id: category.id, tags: updated.tags }
})delete()
Delete a category.
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Category ID to delete |
Returns: void.
k.api.post(() => {
const category = k.commerce.category.create({
title: 'Delete Test',
seoName: 'delete-test-' + Date.now(),
active: true
})
k.commerce.category.delete(category.id)
return { deleted: k.commerce.category.get(category.id) === null }
})getParents()
Get the parent category chain.
| Parameter | Type | Required | Description |
|---|---|---|---|
seoNameOrId | string | Yes | Category SEO name or category ID |
Returns: CategorySimple[] | null. Ancestor categories ordered from the immediate parent up to the root ([parent, ..., root]). Returns null when seoNameOrId is empty or whitespace, or [] when no category matches or the category has no parent.
k.api.get(() => {
const parents = k.commerce.category.getParents('category-seo-name')
return { count: parents ? parents.length : 0, parents }
})addProduct()
Add a product to a category.
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Category ID |
productId | string | Yes | Product ID |
Returns: void.
k.api.post(() => {
const category = k.commerce.category.list({ includeOffline: true })[0]
const product = k.commerce.product.list()[0]
k.commerce.category.addProduct(category.id, product.id)
const products = k.commerce.product.list({ categories: [category.id] })
return { added: products.some(p => p.id === product.id) }
})removeProduct()
Remove a product from a category.
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Category ID |
productId | string | Yes | Product ID |
Returns: void.
k.api.post(() => {
const category = k.commerce.category.list({ includeOffline: true })[0]
const product = k.commerce.product.list()[0]
k.commerce.category.removeProduct(category.id, product.id)
const products = k.commerce.product.list({ categories: [category.id] })
return { removed: !products.some(p => p.id === product.id) }
})Structures
CategorySimple Properties
| Property | Description | Type |
|---|---|---|
id | Category ID | string |
title | Category title | string |
seoName | SEO name | string |
description | Category description | string |
image | Category image | string |
imageMeta | Image metadata | string |
active | Whether active | boolean |
tags | Tags | string[] |
parentId | Parent category ID | string |
CategoryDetail Properties
| Property | Description | Type |
|---|---|---|
id | Category ID | string |
title | Category title | string |
seoName | SEO name | string |
description | Category description | string |
image | Category image | string |
imageMeta | Image metadata | string |
active | Whether active | boolean |
parentId | Parent category ID | string |
tags | Tags | string[] |
path | Full path from root down to the current category (inclusive of current) | CategorySimple[] |
children | Subcategory list | CategorySimple[] |
TypeScript Definitions
Show full TypeScript definitions
interface KCategory {
list(options?: CategoryQueryOptions): CategorySimple[];
get(seoNameOrId: string): CategoryDetail;
getParents(seoNameOrId: string): CategorySimple[];
create(value: NewCategory): Category;
updateField(id: string, property: string, value: any, lang?: string): void;
addProduct(id: string, productId: string): void;
removeProduct(id: string, productId: string): void;
updateFields(id: string, updateFields: UpdateFieldParams[]): void;
delete(id: string): void;
}
interface CategoryQueryOptions {
includeOffline: boolean;
where: any;
}
interface CategorySimple {
id: string;
title: string;
seoName: string;
description: string;
image: string;
imageMeta: string;
active: boolean;
tags: string[];
parentId: string;
}
interface CategoryDetail {
id: string;
title: string;
seoName: string;
description: string;
image: string;
imageMeta: string;
active: boolean;
parentId: string;
tags: string[];
path: CategorySimple[];
children: CategorySimple[];
}
interface NewCategory {
title: string;
seoName: string;
description: string;
image: string;
active: boolean;
parentId: string;
tags: string[];
}
interface UpdateFieldParams {
property: string;
value: any;
lang: string;
}Related Docs
- k.commerce - E-commerce module overview
- product - Product management
- cart - Shopping cart