Skip to content

category

Product Category Management - Category CRUD and Search

Overview

k.commerce.category provides operations related to product classification, including creation, query, update, delete, obtaining parent classification, etc.

TypeScript definition

ts
interface KCategory {
    list(query?: CategoryQueryParams): Category[];
    get(seoNameOrId: string): Category;
    create(category: NewCategory): Category;
    delete(categoryId: string): void;
    updateField(categoryId: string, field: string, value: any, lang?: string): void;
    updateFields(categoryId: string, updateContents: UpdateContent[]): void;
    addProduct(categoryId: string, productId: string): void;
    removeProduct(categoryId: string, productId: string): void;
    getParents(seoNameOrId: string): Category[];
}

core method

list()

Get a list of categories.

Parameters:

ParameterTypeRequiredDescription
queryCategoryQueryParamsNoCategory list filters.
query.includeOfflinebooleanNoWhether to include offline categories. Default is false.

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

ts
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

When the query cannot find a category, null is returned.

Parameters:

ParameterTypeRequiredDescription
seoNameOrIdstringYesCategory SEO name or category ID.

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

ts
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 categories.

Parameters:

ParameterTypeRequiredDescription
categoryNewCategoryYesNew category data.

Returns: Category. Created category object.

ts
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 field.

Parameters:

ParameterTypeRequiredDescription
categoryIdstringYesCategory ID.
fieldstringYesField name.
valueanyYesField value.
langstringNoLanguage code for multilingual fields.

Returns: void.

ts
k.api.post(() => {
    k.commerce.category.updateField(categoryId, "title", "Updated Title")
    return { success: true }
})

// Multilingual update
k.api.post(() => {
    k.commerce.category.updateField(categoryId, "title", "New Title", "en")
    return { success: true }
})

updateFields()

Update fields in batches.

Parameters:

ParameterTypeRequiredDescription
categoryIdstringYesCategory ID.
updateContentsUpdateContent[]YesList of fields to update.
updateContents[].propertystringYesField name.
updateContents[].valueanyYesField value.
updateContents[].langstringNoLanguage code for multilingual fields.

Returns: void.

ts
k.api.post(() => {
    k.commerce.category.updateFields(categoryId, [
        { property: "title", value: "Updated Title", lang: "en" },
        { property: "tags", value: ["tag1", "tag2"] }
    ])
    return { success: true }
})

delete()

Delete categories.

Parameters:

ParameterTypeRequiredDescription
categoryIdstringYesCategory ID to delete.

Returns: void.

ts
k.api.post(() => {
    k.commerce.category.delete(categoryId)
    return { success: true }
})

getParents()

Get the parent category chain.

Parameters:

ParameterTypeRequiredDescription
seoNameOrIdstringYesCategory SEO name or category ID.

Returns: Category[]. Parent category chain from the current category upward.

ts
k.api.get(() => {
    const parents = k.commerce.category.getParents('category-seo-name')
    return { count: parents.length, parents: parents }
})

addProduct()

Add products to categories.

Parameters:

ParameterTypeRequiredDescription
categoryIdstringYesCategory ID.
productIdstringYesProduct ID.

Returns: void.

ts
k.api.post(() => {
    k.commerce.category.addProduct(categoryId, productId)
    return { success: true }
})

removeProduct()

Remove product from category.

Parameters:

ParameterTypeRequiredDescription
categoryIdstringYesCategory ID.
productIdstringYesProduct ID.

Returns: void.

ts
k.api.post(() => {
    k.commerce.category.removeProduct(categoryId, productId)
    return { success: true }
})

Classification structure

Category property

PropertyDescriptionType
IDCategory IDstring
titleCategory namestring
descriptionClassification descriptionstring
imageClassified picturesstring
seoNameSEO namestring
parentIdParent category IDstring
tagsLabelstring[]
activeWhether to enableboolean
ordersortnumber
createdAtcreation timeDate
updatedAtUpdate timeDate
childrenSubcategory listCategory[]