Skip to content

k.commerce.category ​

Product category management.

Execution ContextPrerequisitesTypical Use
KScript API (k.api.get / k.api.post), server-side <script env="server">E-commerce module enabledQuery 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 ​

js
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 / PropertyDescriptionReturns
list(options?)List categories with optional offline inclusion and where filterCategorySimple[]
get(seoNameOrId)Get category details including ancestor path and child categoriesCategoryDetail | 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 categoryCategory
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 categoryvoid
removeProduct(id, productId)Remove a product from the categoryvoid
delete(id)Delete a categoryvoid

list() ​

Get a list of categories.

ParameterTypeRequiredDescription
optionsCategoryQueryOptionsNoCategory list filters
options.includeOfflinebooleanNoWhether to include offline categories (default false)
options.whereanyNoFilter condition expression

Returns: CategorySimple[]. Category list matching the filters.

js
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.

ParameterTypeRequiredDescription
seoNameOrIdstringYesCategory SEO name or category ID

Returns: CategoryDetail | null. Matching category details, or null when it does not exist.

js
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.

ParameterTypeRequiredDescription
valueNewCategoryYesNew category data

Returns: Category. Created category entity object.

js
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.

ParameterTypeRequiredDescription
idstringYesCategory ID
propertystringYesField name
valueanyYesField value
langstringNoLanguage code for multilingual fields

Returns: void.

js
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.

ParameterTypeRequiredDescription
idstringYesCategory ID
updateFieldsUpdateFieldParams[]YesList of field updates
updateFields[].propertystringYesField name
updateFields[].valueanyYesField value
updateFields[].langstringNoLanguage code for multilingual fields

Returns: void.

js
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.

ParameterTypeRequiredDescription
idstringYesCategory ID to delete

Returns: void.

js
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.

ParameterTypeRequiredDescription
seoNameOrIdstringYesCategory 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.

js
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.

ParameterTypeRequiredDescription
idstringYesCategory ID
productIdstringYesProduct ID

Returns: void.

js
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.

ParameterTypeRequiredDescription
idstringYesCategory ID
productIdstringYesProduct ID

Returns: void.

js
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 ​

PropertyDescriptionType
idCategory IDstring
titleCategory titlestring
seoNameSEO namestring
descriptionCategory descriptionstring
imageCategory imagestring
imageMetaImage metadatastring
activeWhether activeboolean
tagsTagsstring[]
parentIdParent category IDstring

CategoryDetail Properties ​

PropertyDescriptionType
idCategory IDstring
titleCategory titlestring
seoNameSEO namestring
descriptionCategory descriptionstring
imageCategory imagestring
imageMetaImage metadatastring
activeWhether activeboolean
parentIdParent category IDstring
tagsTagsstring[]
pathFull path from root down to the current category (inclusive of current)CategorySimple[]
childrenSubcategory listCategorySimple[]

TypeScript Definitions ​

Show full TypeScript definitions
ts
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;
}