k.commerce.product
Product management.
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
KScript API (k.api.get / k.api.post), server-side <script env="server"> | E-commerce module enabled; file digital items read files via k.file site storage paths | List/search catalog products, manage SKU variants, update multilingual fields, compute customer/currency pricing, attach digital assets, and handle reviews |
k.commerce.product manages catalog products, variants, category memberships, currency/customer price resolution, variant-level digital items, and customer reviews.
Quick Start
k.api.get(() => {
const products = k.commerce.product.list({ includeOffline: false })
if (products.length === 0) {
return { count: 0, first: null }
}
const detail = k.commerce.product.get(products[0].id)
return {
count: products.length,
title: detail.title,
variantCount: detail.variants.length
}
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
list(query?) | List products filtered by categories, currency, offline flag, or where | ProductSimple[] |
get(seoNameOrId, options?) | Get product details and variants by SEO name or ID (throws if missing) | ProductDetail |
search(keyword, options?) | Search products by keyword and facet filters with facet aggregation | SearchResult |
create(value) | Create a new product | Product |
createVariant(productId, value) | Create a new variant under an existing product | Product |
updateField(id, property, value, lang?) | Update a single product field (optionally localized) | void |
updateFields(id, updateFields) | Batch update multiple product fields | void |
updateVariantField(id, property, value, lang?) | Update a single variant field (9 supported fields; lang is ignored) | void |
updateVariantFields(id, updateFields) | Batch update multiple variant fields | void |
getPriceDetail(variantId, options?) | Calculate discounted variant price for a customer/currency | ProductPrice | null |
addCategory(id, categoryId) | Assign the product to a category | void |
removeCategory(id, categoryId) | Remove the product from a category | void |
addTextDigitalItem(variantId, name, text) | Attach a plain-text digital delivery item to a variant | void |
addLinkDigitalItem(variantId, name, link) | Attach a URL link digital delivery item to a variant | void |
addFileDigitalItem(variantId, name, filePath) | Attach a site file digital delivery item to a variant | void |
removeDigitalItem(variantId, digitalItemId) | Remove a digital delivery item from a variant | void |
addReview(model) | Submit a customer review for a product variant | void |
reviewList(variantId) | List customer reviews for a product variant | ProductReviewModel[] |
deleteVariant(id) | Delete a variant by ID | void |
delete(id) | Delete a product by ID | void |
list()
Get a list of products.
| Parameter | Type | Required | Description |
|---|---|---|---|
query | ProductQueryParams | No | Product list filters |
query.categories | string[] | No | Filter by category IDs |
query.includeOffline | boolean | No | Whether to include offline products (default false) |
query.includeSubCategory | boolean | No | Whether to include products from child categories (default false) |
query.currency | string | No | Target display currency code |
query.where | any | No | Custom filter expression/object |
Returns: ProductSimple[]. Product list matching the filters.
k.api.get(() => {
const products = k.commerce.product.list()
return { count: products.length, products }
})get()
Get product details including variants, categories, and currency conversion.
Exception Handling
Throws "Product not found" when no matching product exists; wrap calls with try / catch when looking up arbitrary IDs or slugs.
| Parameter | Type | Required | Description |
|---|---|---|---|
seoNameOrId | string | Yes | Product SEO name or product ID |
options | GetProductOptions | No | Optional query options |
options.currency | string | No | Target currency code for converted prices |
Returns: ProductDetail. Product detail including variants, categories, and currency.
k.api.get(() => {
try {
const product = k.commerce.product.get('product-seo-name')
return { title: product.title, variants: product.variants }
} catch (e) {
return { error: e.message }
}
})search()
Search products by keyword and facet filters.
| Parameter | Type | Required | Description |
|---|---|---|---|
keyword | string | Yes | Search keyword |
options | SearchOptions | No | Search filters |
options.categories | string[] | No | Filter by category IDs |
options.includeOffline | boolean | No | Whether to include offline products (default false) |
options.includeSubCategory | boolean | No | Whether to include products from child categories (default false) |
options.currency | string | No | Target display currency code |
options.facetParams | FacetParam[] | No | Facet filter items ({ name, label }) |
Returns: SearchResult. Contains matching list and aggregated facets.
k.api.get(() => {
const result = k.commerce.product.search('wireless earbuds', {
includeOffline: true,
includeSubCategory: false
})
return { count: result.list.length, facets: result.facets }
})create()
Create a new product (and automatically create its initial default variant using price, inventory, variantImage, autoDelivery, and active).
| Parameter | Type | Required | Description |
|---|---|---|---|
value | NewProduct | Yes | New product data |
Returns: Product (Entities.Product). Does not include price, inventory, autoDelivery, variantImage, variants, or categories; call get(product.id) for ProductDetail when you need variants or prices.
k.api.post(() => {
const product = k.commerce.product.create({
title: 'New Product',
description: 'Product description',
price: 99.9,
active: true
})
return { id: product.id, title: product.title }
})createVariant()
Create a new variant for an existing product.
| Parameter | Type | Required | Description |
|---|---|---|---|
productId | string | Yes | Product ID |
value | NewProductVariant | Yes | New variant data |
Returns: Product (Entities.Product). Does not include price, inventory, variants, or categories; call get(product.id) to inspect ProductDetail.
k.api.post(() => {
const productId = k.commerce.product.list()[0].id
const product = k.commerce.product.createVariant(productId, {
sku: 'SKU-' + Date.now(),
price: 88,
inventory: 100
})
const detail = k.commerce.product.get(product.id)
return { id: product.id, variantCount: detail.variants.length }
})updateField()
Update a single product field.
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Product 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 productId = k.commerce.product.list()[0].id
k.commerce.product.updateField(productId, 'title', 'Updated Title')
k.commerce.product.updateField(productId, 'title', 'New Title', 'en')
return { success: true }
})updateFields()
Batch update multiple product fields.
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Product ID |
updateFields | UpdateFieldParams[] | Yes | List of fields to update |
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 productId = k.commerce.product.list()[0].id
k.commerce.product.updateFields(productId, [
{ property: 'title', value: 'Updated Title', lang: 'en' },
{ property: 'tags', value: ['tag1', 'tag2'] }
])
return { success: true }
})updateVariantField()
Update a single variant field. Only the following 9 fixed property names (case-insensitive) are supported: active, barcode, sku, image, tags, price, weight, inventory, and selectedOptions.
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Variant ID |
property | string | Yes | Field name (one of the 9 supported fields above) |
value | any | Yes | Field value |
lang | string | No | Language code (accepted by signature for consistency with updateField, but ignored at runtime because variants have no multilingual fields) |
Returns: void.
k.api.post(() => {
const product = k.commerce.product.get(k.commerce.product.list()[0].id)
const variantId = product.variants[0].id
k.commerce.product.updateVariantField(variantId, 'sku', 'NEW-SKU')
return { updated: true }
})updateVariantFields()
Batch update multiple variant fields. Only the 9 fixed fields (active, barcode, sku, image, tags, price, weight, inventory, selectedOptions) are supported.
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Variant ID |
updateFields | UpdateFieldParams[] | Yes | List of fields to update |
updateFields[].property | string | Yes | Field name (one of the 9 supported fields above) |
updateFields[].value | any | Yes | Field value |
updateFields[].lang | string | No | Language code (accepted by signature, but ignored at runtime because variants have no multilingual fields) |
Returns: void.
k.api.post(() => {
const product = k.commerce.product.get(k.commerce.product.list()[0].id)
const variantId = product.variants[0].id
k.commerce.product.updateVariantFields(variantId, [
{ property: 'sku', value: 'NEW-SKU' }
])
return { updated: true }
})getPriceDetail()
Calculate discounted price details for a variant including original amount, discounted amount, and currency metadata.
Note: Returned
amountandoriginalAmountvalues are always calculated in the store's base currency (not multiplied bycurrency.rate).options.currencyonly resolves the returnedcurrencymetadata object ({ code, symbol, rate }); multiply bydetail.currency.rateon the caller side when displaying converted currency amounts.
| Parameter | Type | Required | Description |
|---|---|---|---|
variantId | string | Yes | Variant ID |
options | ProductPriceOptions | No | Price calculation options (customerId for membership/discount matching, currency to populate currency metadata) |
Returns: ProductPrice | null. Returns base-currency amount, originalAmount, and resolved currency ({ code, symbol, rate }), or null if the variant does not exist.
k.api.get(() => {
const product = k.commerce.product.get(k.commerce.product.list()[0].id)
const variantId = product.variants[0].id
const detail = k.commerce.product.getPriceDetail(variantId, {
customerId: k.commerce.customer.currentId || '',
currency: 'USD'
})
return detail
})addCategory()
Assign the product to a category (by productId).
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Product ID |
categoryId | string | Yes | Category ID |
Returns: void.
k.api.post(() => {
const productId = k.commerce.product.list()[0].id
const categoryId = k.commerce.category.list()[0].id
k.commerce.product.addCategory(productId, categoryId)
return { categories: k.commerce.product.get(productId).categories }
})removeCategory()
Remove the product from a category.
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Product ID |
categoryId | string | Yes | Category ID |
Returns: void.
k.api.post(() => {
const productId = k.commerce.product.list()[0].id
const categoryId = k.commerce.category.list()[0].id
k.commerce.product.removeCategory(productId, categoryId)
return { categories: k.commerce.product.get(productId).categories }
})addTextDigitalItem()
Attach a plain-text digital delivery item (such as a license key) to a variant (variantId).
| Parameter | Type | Required | Description |
|---|---|---|---|
variantId | string | Yes | Variant ID |
name | string | Yes | Digital item name shown to the customer |
text | string | Yes | Text content |
Returns: void.
k.api.post(() => {
const product = k.commerce.product.get(k.commerce.product.list()[0].id)
const variantId = product.variants[0].id
k.commerce.product.addTextDigitalItem(variantId, 'License key', 'XXXX-XXXX')
return { updated: true }
})addLinkDigitalItem()
Attach a URL link digital delivery item to a variant.
| Parameter | Type | Required | Description |
|---|---|---|---|
variantId | string | Yes | Variant ID |
name | string | Yes | Digital item name shown to the customer |
link | string | Yes | Download or resource URL |
Returns: void.
k.api.post(() => {
const product = k.commerce.product.get(k.commerce.product.list()[0].id)
const variantId = product.variants[0].id
k.commerce.product.addLinkDigitalItem(variantId, 'Download', 'https://example.com/file.zip')
return { updated: true }
})addFileDigitalItem()
Attach a site file digital delivery item (for example an e-book) to a variant. The file path is a site directory path readable by k.file.
| Parameter | Type | Required | Description |
|---|---|---|---|
variantId | string | Yes | Variant ID |
name | string | Yes | Digital item filename (used as the display name, stored filename on disk, and for MIME type detection; must include the file extension such as 'book.pdf' and must not contain path separators) |
filePath | string | Yes | Relative path to the site file |
Returns: void.
k.api.post(() => {
const product = k.commerce.product.get(k.commerce.product.list()[0].id)
const variantId = product.variants[0].id
k.commerce.product.addFileDigitalItem(variantId, 'book.pdf', 'exports/book.pdf')
return { updated: true }
})removeDigitalItem()
Remove a digital delivery item from a variant by its ID.
| Parameter | Type | Required | Description |
|---|---|---|---|
variantId | string | Yes | Variant ID |
digitalItemId | string | Yes | Digital item ID |
Returns: void.
k.api.post(() => {
k.commerce.product.removeDigitalItem('variant-id', 'digital-item-id')
return { removed: true }
})addReview()
Submit a customer review for a product variant.
| Parameter | Type | Required | Description |
|---|---|---|---|
model | NewProductReview | Yes | Review payload |
model.customerId | string | Yes | Customer ID |
model.variantId | string | Yes | Variant ID |
model.rating | number | Yes | Rating score |
model.comment | string | Yes | Review text |
model.images | string[] | No | Image URL list (note: currently ignored and not persisted by addReview(), nor returned by reviewList()) |
Note: Although
imagesis declared onNewProductReview, the currentKProduct.AddReviewimplementation does not persistimages, andProductReviewModelreturned byreviewList()does not store or include them.
Returns: void.
k.api.post(() => {
const customerId = k.commerce.customer.currentId || 'customer-id'
const product = k.commerce.product.get(k.commerce.product.list()[0].id)
const variantId = product.variants[0].id
k.commerce.product.addReview({
customerId,
variantId,
rating: 5,
comment: 'Great quality',
images: []
})
return k.commerce.product.reviewList(variantId)
})reviewList()
List customer reviews for a product variant.
| Parameter | Type | Required | Description |
|---|---|---|---|
variantId | string | Yes | Variant ID |
Returns: ProductReviewModel[] (customerId, productId, variantId, rating, comment, createdAt).
k.api.get(() => {
const product = k.commerce.product.get(k.commerce.product.list()[0].id)
const reviews = k.commerce.product.reviewList(product.variants[0].id)
return { count: reviews.length, items: reviews }
})deleteVariant()
Delete a product variant.
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Variant ID |
Returns: void.
k.api.post(() => {
k.commerce.product.deleteVariant('variant-id')
return { deleted: true }
})delete()
Delete a product.
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Product ID to delete |
Returns: void.
k.api.post(() => {
k.commerce.product.delete('product-id')
return { deleted: true }
})Structures
Product attributes
create() and createVariant() return the Kooboo.Sites.Commerce.Entities.Product entity directly (it does not include price, inventory, autoDelivery, variantImage, variants, or categories; call get(product.id) to retrieve ProductDetail):
| Property | Description | Type |
|---|---|---|
id | Product ID | string |
title | Product title | string |
description | Product description | string |
attributes | Attribute key-value pairs | { key: string, value: string }[] |
active | Whether active | boolean |
seoName | SEO name | string |
featuredImage | Featured image | string |
images | Image list | string[] |
imageMetas | Image metadata JSON | string |
tags | Tags | string[] |
isDigital | Whether digital product | boolean |
maxDownloadCount | Maximum number of downloads (digital products) | number | null |
maxDownloadDay | Maximum download days (digital products) | number | null |
variantOptions | Variant option definitions | VariantOption[] |
customData | Multilingual and custom data map | Record<string, Record<string, any>> |
createdAt | Creation time | Date |
updatedAt | Update time | Date |
NewProduct properties
Input payload for create(value) (price, inventory, variantImage, autoDelivery, and active populate the automatically created first variant):
| Property | Description | Type |
|---|---|---|
title | Product title | string |
description | Product description | string |
attributes | Attribute key-value pairs | { key: string, value: string }[] |
active | Whether active (also applied to the default variant) | boolean |
seoName | SEO name | string |
featuredImage | Featured image | string |
images | Image list | string[] |
tags | Tags | string[] |
variantImage | Default variant image | string |
price | Price (applied to the default variant) | number |
inventory | Inventory count (applied to the default variant) | number |
isDigital | Whether digital product | boolean |
autoDelivery | Whether delivered automatically (applied to the default variant) | boolean |
maxDownloadCount | Maximum number of downloads (digital products) | number | null |
maxDownloadDay | Maximum download days (digital products) | number | null |
ProductDetail properties
Detailed view returned by get(seoNameOrId, options?) (also includes dynamic custom fields configured in backend settings):
| Property | Description | Type |
|---|---|---|
id | Product ID | string |
title | Product title | string |
description | Product description | string |
featuredImage | Featured image | string |
active | Whether active | boolean |
isDigital | Whether digital product | boolean |
price | Lowest converted variant price | number |
images | Image list | string[] |
imageMetas | Image metadata JSON | string |
seoName | SEO name | string |
tags | Tags | string[] |
attributes | Attribute key-value pairs | { key: string, value: string }[] |
variants | Variant list | Variant[] |
categories | Category list | CategorySimple[] |
variantOptions | Localized variant option definitions | VariantOption[] |
currency | Resolved display currency info | { code: string, symbol: string, rate: number } |
Variant properties
| Property | Description | Type |
|---|---|---|
id | Variant ID | string |
productId | Product ID | string |
createdAt | Creation time (serialized date string at runtime via ToKscriptModel) | string |
updatedAt | Update time (serialized date string at runtime via ToKscriptModel) | string |
sku | SKU code | string |
barcode | Barcode | string |
price | Price | number |
inventory | Inventory count | number |
weight | Weight | number |
order | Sort order | number |
sales | Sales volume | number |
active | Whether enabled | boolean |
image | Variant image | string |
imageMeta | Variant image metadata JSON | string |
selectedOptions | Options (e.g. color/size) | { name: string, value: string }[] |
digitals | Digital items list | DigitalItem[] |
DigitalItem property
| Property | Description | Type |
|---|---|---|
type | Item type | 'file' | 'text' | 'link' |
name | Display name | string |
value | Content (text / link / variant archive ID) | string |
TypeScript Definitions
Show full TypeScript definitions
interface KProduct {
list(query?: KScript.Commerce.Models.ProductQueryParams): KScript.Commerce.Models.ProductSimple[];
get(seoNameOrId: string, options?: KScript.Commerce.Models.GetProductOptions): KScript.Commerce.Models.ProductDetail;
search(keyword: string, options?: KScript.Commerce.Models.SearchOptions): KScript.Commerce.Models.SearchResult;
create(value: KScript.Commerce.Models.NewProduct): Kooboo.Sites.Commerce.Entities.Product;
createVariant(productId: string, value: KScript.Commerce.Models.NewProductVariant): Kooboo.Sites.Commerce.Entities.Product;
deleteVariant(id: string): void;
delete(id: string): void;
updateField(id: string, property: string, value: any, lang?: string): void;
updateFields(id: string, updateFields: KScript.Commerce.Models.UpdateFieldParams[]): void;
addCategory(id: string, categoryId: string): void;
removeCategory(id: string, categoryId: string): void;
updateVariantField(id: string, property: string, value: any, lang?: string): void;
updateVariantFields(id: string, updateFields: KScript.Commerce.Models.UpdateFieldParams[]): void;
getPriceDetail(variantId: string, options?: KScript.Commerce.Models.ProductPriceOptions): KScript.Commerce.Models.ProductPrice;
addTextDigitalItem(variantId: string, name: string, text: string): void;
addLinkDigitalItem(variantId: string, name: string, link: string): void;
addFileDigitalItem(variantId: string, name: string, filePath: string): void;
removeDigitalItem(variantId: string, digitalItemId: string): void;
addReview(model: KScript.Commerce.Models.NewProductReview): void;
reviewList(variantId: string): Kooboo.Sites.Commerce.DataStorage.ProductReviewModel[];
}
interface ProductQueryParams {
categories: string[];
includeSubCategory: boolean;
includeOffline: boolean;
currency: string;
where: any;
}
interface Product {
id: string;
title: string;
description: string;
attributes: { key: string; value: string }[];
active: boolean;
seoName: string;
featuredImage: string;
images: string[];
imageMetas: string;
tags: string[];
isDigital: boolean;
maxDownloadCount?: number;
maxDownloadDay?: number;
variantOptions: Kooboo.Sites.Commerce.Entities.VariantOption[];
customData: Record<string, Record<string, any>>;
createdAt: Date;
updatedAt: Date;
}
interface ProductSimple {
id: string;
title: string;
featuredImage: string;
images: string[];
imageMetas: string;
active: boolean;
isDigital: boolean;
price: number;
seoName: string;
tags: string[];
attributes: any[];
variants: Variant[];
variantOptions: VariantOption[];
currency: Kooboo.Sites.Commerce.Entities.Currency;
}
interface GetProductOptions {
currency: string;
}
interface ProductDetail {
id: string;
title: string;
description: string;
featuredImage: string;
active: boolean;
isDigital: boolean;
price: number;
images: string[];
imageMetas: string;
seoName: string;
tags: string[];
attributes: KScript.KeyValue[];
variants: Variant[];
categories: CategorySimple[];
variantOptions: VariantOption[];
currency: Kooboo.Sites.Commerce.Entities.Currency;
}
interface SearchOptions {
facetParams: FacetParam[];
categories: string[];
includeSubCategory: boolean;
includeOffline: boolean;
currency: string;
}
interface SearchResult {
list: ProductSimple[];
facets: FacetResult[];
}
interface NewProduct {
title: string;
description: string;
attributes: KScript.KeyValue[];
active: boolean;
seoName: string;
featuredImage: string;
images: string[];
tags: string[];
variantImage: string;
price: number;
inventory: number;
isDigital: boolean;
autoDelivery: boolean;
maxDownloadCount?: number;
maxDownloadDay?: number;
}
interface NewProductVariant {
barcode: string;
sku: string;
image: string;
price: number;
active: boolean;
selectedOptions: Kooboo.Sites.Commerce.Entities.ProductVariantOption[];
weight: number;
inventory: number;
tags: string[];
autoDelivery: boolean;
}
interface ProductPriceOptions {
customerId: string;
currency: string;
}
interface ProductPrice {
currency: Kooboo.Sites.Commerce.Entities.Currency;
originalAmount: number;
amount: number;
}
interface NewProductReview {
customerId: string;
variantId: string;
rating: number;
comment: string;
images: string[];
}
interface ProductReviewModel {
customerId: string;
productId: string;
variantId: string;
rating: number;
comment: string;
createdAt: Date;
}
interface DigitalItem {
/** 'file' 'text' 'link' */
type: string;
name: string;
value: string;
}Related Docs
- k.commerce - E-commerce module overview
- category - Product categories
- cart - Shopping cart
- order - Order management