k.commerce.order
Order management.
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
KScript API (k.api.get / k.api.post), server-side <script env="server"> | Cart-based creation requires lines added via k.commerce.cart; direct creation requires explicit lines | Query and filter orders, create orders at checkout, record payments, fulfill whole or split shipments, and update addresses or tracking info |
k.commerce.order provides full order lifecycle operations: paginated order lookup, order creation from a cart or CreateOrderInit payload, payment marking, full or line-level fulfillment, address/shipping/extension updates, cancellation, reopening, and deletion.
Quick Start
k.api.get(() => {
const result = k.commerce.order.list({
paid: false,
canceled: false,
pageIndex: 1,
pageSize: 10
})
return { count: result.count, items: result.list }
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
list(query?) | Query paginated orders (runtime returns OrderResult) | OrderResult |
get(orderId) | Get full order details by ID | OrderDetail | null |
create(cartId, options) | Create an order from an existing shopping cart | OrderDetail |
create(init) | Create a standalone order directly from CreateOrderInit | OrderDetail |
pay(orderId, method) | Mark the order as paid with the given payment method | void |
delivery(orderId, shippingCarrier, trackingNumber) | Mark the full order as shipped with carrier and tracking number | void |
delivery(orderId, lineIds) | Mark specific order line IDs as shipped (partial fulfillment) | void |
updateShippingInfo(orderId, orderLineId, options) | Update shipping details or digital items for an order line | void |
updateAddress(orderId, address) | Update the order shipping address | void |
updateExtensionField(id, property, value) | Update an existing key in order.extensionFields | void |
cancel(orderId, reason) | Cancel an order with a reason | void |
reopen(orderId) | Reopen a canceled order | void |
delete(id) | Permanently delete an order | void |
list()
Get a paginated list of orders.
Note: At runtime
k.commerce.order.list()returns anOrderResultpagination object ({ list, count, pageIndex, pageSize }), whereas the IDE type definition currently declaresOrderDetail[].
| Parameter | Type | Required | Description |
|---|---|---|---|
query | OrderQuery | No | Filter and pagination options |
query.customerId | string | No | Customer ID |
query.paid | boolean | No | Whether paid |
query.delivered | boolean | No | Whether delivered |
query.canceled | boolean | No | Whether canceled |
query.startDate | Date | No | Start date |
query.endDate | Date | No | End date |
query.pageIndex | number | No | Page number, default 1 |
query.pageSize | number | No | Items per page, default 30 |
Returns: OrderResult. Contains list (OrderDetail[]), count, pageIndex, and pageSize.
k.api.get(() => {
const result = k.commerce.order.list({
startDate: new Date('2025-01-01'),
endDate: new Date('2025-12-31'),
paid: false,
delivered: false,
canceled: false,
pageIndex: 1,
pageSize: 10
})
return result
})get()
Get details for a specific order.
| Parameter | Type | Required | Description |
|---|---|---|---|
orderId | string | Yes | Order ID |
Returns: OrderDetail | null.
k.api.get(() => {
const order = k.commerce.order.get('order-id')
return { id: order.id, totalAmount: order.totalAmount }
})create()
Create an order from an existing cart, or create a standalone order directly with CreateOrderInit.
Parameters (create(cartId, options)):
| Parameter | Type | Required | Description |
|---|---|---|---|
cartId | string | Yes | Cart ID |
options | CreateOrderOptions | Yes | Order creation options |
options.address | Address | Yes | Shipping address |
options.currency | string | No | Order currency code |
options.extensionFields | KeyValue[] | No | Extension fields |
options.note | string | No | Order note |
options.scheduledDeliveryTime | Date | No | Scheduled delivery time |
Parameters (create(init)):
| Parameter | Type | Required | Description |
|---|---|---|---|
init | CreateOrderInit | Yes | Direct order payload |
init.lines | CreateOrderLineInit[] | Yes | Order line items |
init.address | Address | No | Shipping address |
init.currency | string | No | Currency code |
init.shippingAmount | number | No | Shipping amount |
init.customerId | string | No | Existing customer ID (customer email, name, and phone on the order are loaded solely from this matched customer record) |
init.email | string | No | Customer email (currently ignored by OrderService.Create; populated only from the customer matched by customerId) |
init.firstName | string | No | Customer first name (currently ignored; populated only from customerId) |
init.lastName | string | No | Customer last name (currently ignored; populated only from customerId) |
init.phone | string | No | Customer phone (currently ignored; populated only from customerId) |
init.source | string | No | Order source |
init.note | string | No | Order note |
init.extensionButton | ExtensionButton | No | Extension button ({ text, url }) |
init.extensionFields | KeyValue[] | No | Extension fields |
Returns: OrderDetail.
k.api.post(() => {
// Overload 1: Create an order from a cart
const cartId = k.commerce.cart.create()
const product = k.commerce.product.create({
title: 'Order Test Product',
description: 'Test',
price: 99.9,
active: true,
seoName: 'order-test-' + Date.now(),
featuredImage: ''
})
k.commerce.product.createVariant(product.id, {
sku: 'ORD-SKU-' + Date.now(),
price: 99.9,
inventory: 100,
barcode: '',
active: true,
selectedOptions: []
})
const refreshed = k.commerce.product.get(product.id)
k.commerce.cart.addOrUpdateLine(cartId, refreshed.variants[0].id, 1)
const order = k.commerce.order.create(cartId, {
address: {
country: 'China',
province: 'Fujian',
city: 'Xiamen',
address1: 'address1',
address2: '',
zip: '361000',
firstName: 'firstName',
lastName: 'lastName',
phone: '13800138000',
isDefault: true
},
currency: 'USD',
note: 'test order',
extensionFields: []
})
return { orderId: order.id, totalAmount: order.totalAmount }
})Note: When creating an order via
create(init), customer contact fields (firstName,lastName,phone) on the order are populated solely from the customer record matched byinit.customerId; top-levelfirstName,lastName, andphoneoninitare currently ignored byOrderService.Create.
k.api.post(() => {
// Overload 2: Create a standalone order directly via CreateOrderInit
const customer = k.commerce.customer.getByEmail('buyer@example.com')
const order = k.commerce.order.create({
customerId: customer ? customer.id : '',
currency: 'USD',
shippingAmount: 10,
source: 'api',
note: 'Direct order',
address: {
country: 'China',
province: 'Fujian',
city: 'Xiamen',
address1: 'address1',
address2: '',
zip: '361000',
firstName: 'Ada',
lastName: 'Lovelace',
phone: '13800138000',
isDefault: true
},
lines: [
{
title: 'Custom Service',
price: 50,
quantity: 2,
image: '',
extensionButton: { text: '', url: '' }
}
],
extensionButton: { text: '', url: '' },
extensionFields: [{ key: 'channel', value: 'web' }]
})
return { orderId: order.id, totalAmount: order.totalAmount }
})cancel()
Cancel an order.
| Parameter | Type | Required | Description |
|---|---|---|---|
orderId | string | Yes | Order ID |
reason | string | Yes | Reason for cancellation |
Returns: void.
k.api.post(() => {
const orderId = 'order-id'
k.commerce.order.cancel(orderId, 'cancel reason')
const order = k.commerce.order.get(orderId)
return { id: order.id, canceled: order.canceled, cancelReason: order.cancelReason }
})delete()
Delete an order.
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Order ID |
Returns: void.
k.api.post(() => {
const orderId = 'order-id'
k.commerce.order.delete(orderId)
return { deleted: k.commerce.order.get(orderId) === null }
})pay()
Mark an order as paid.
| Parameter | Type | Required | Description |
|---|---|---|---|
orderId | string | Yes | Order ID |
method | string | Yes | Payment method |
Returns: void.
k.api.post(() => {
const orderId = 'order-id'
k.commerce.order.pay(orderId, 'alipay')
const order = k.commerce.order.get(orderId)
return { id: order.id, paid: order.paid, paymentMethod: order.paymentMethod }
})delivery()
Mark the full order as shipped with carrier/tracking details, or mark specific order lines as shipped.
Parameters (delivery(orderId, shippingCarrier, trackingNumber)):
| Parameter | Type | Required | Description |
|---|---|---|---|
orderId | string | Yes | Order ID |
shippingCarrier | string | Yes | Shipping carrier name or code (e.g. 'SF Express' or shipping.name) |
trackingNumber | string | Yes | Tracking number |
Parameters (delivery(orderId, lineIds)):
| Parameter | Type | Required | Description |
|---|---|---|---|
orderId | string | Yes | Order ID |
lineIds | string[] | Yes | Order line IDs to mark as shipped |
Returns: void.
k.api.post(() => {
const orderId = 'order-id'
const shipping = k.commerce.shipping.list()[0]
const carrier = shipping ? (shipping.code || shipping.name) : 'SF Express'
k.commerce.order.delivery(orderId, carrier, 'SF123456789')
const order = k.commerce.order.get(orderId)
return {
id: order.id,
delivered: order.delivered,
trackingNumber: order.lines[0] ? order.lines[0].trackingNumber : null
}
})k.api.post(() => {
const orderId = 'order-id'
const order = k.commerce.order.get(orderId)
const lineIds = order.lines.map(l => l.id)
k.commerce.order.delivery(orderId, lineIds)
const updated = k.commerce.order.get(orderId)
return { id: updated.id, delivered: updated.delivered, partialDelivered: updated.partialDelivered }
})reopen()
Reopen a canceled order.
| Parameter | Type | Required | Description |
|---|---|---|---|
orderId | string | Yes | Order ID |
Returns: void.
k.api.post(() => {
const orderId = 'order-id'
k.commerce.order.reopen(orderId)
const order = k.commerce.order.get(orderId)
return { id: order.id, canceled: order.canceled }
})updateAddress()
Update the order shipping address.
| Parameter | Type | Required | Description |
|---|---|---|---|
orderId | string | Yes | Order ID |
address | Address | Yes | Shipping address |
Returns: void.
k.api.post(() => {
const orderId = 'order-id'
k.commerce.order.updateAddress(orderId, {
country: 'China',
province: 'Fujian',
city: 'Xiamen',
address1: 'address1',
address2: '',
zip: '361000',
firstName: 'firstName',
lastName: 'lastName',
phone: '13800138000',
isDefault: true
})
const order = k.commerce.order.get(orderId)
return { id: order.id, shippingAddress: order.shippingAddress }
})updateShippingInfo()
Update shipping details for an individual order line.
| Parameter | Type | Required | Description |
|---|---|---|---|
orderId | string | Yes | Order ID |
orderLineId | string | Yes | Order item ID |
options | DeliveryOptions | Yes | Shipping options |
options.shippingCarrier | string | No | Shipping carrier name or code (e.g. 'SF Express' or shipping.name) |
options.trackingNumber | string | No | Tracking number |
options.digitalItems | DigitalOrderItem[] | No | Digital product items |
Returns: void.
k.api.post(() => {
const orderId = 'order-id'
const order = k.commerce.order.get(orderId)
const lineId = order.lines[0].id
const shipping = k.commerce.shipping.list()[0]
const carrier = shipping ? (shipping.code || shipping.name) : 'SF Express'
k.commerce.order.updateShippingInfo(orderId, lineId, {
shippingCarrier: carrier,
trackingNumber: 'SF123456789'
})
const updated = k.commerce.order.get(orderId)
return { id: updated.id, lineTracking: updated.lines[0].trackingNumber }
})updateExtensionField()
Update the value of an existing key in order.extensionFields. Only entries whose key already exists on the order are updated.
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | Order ID |
property | string | Yes | Extension field key |
value | string | Yes | Updated field value |
Returns: void.
k.api.post(() => {
const orderId = 'order-id'
k.commerce.order.updateExtensionField(orderId, 'channel', 'mobile')
const updated = k.commerce.order.get(orderId)
return { id: updated.id, extensionFields: updated.extensionFields }
})Structures
OrderResult Properties
| Property | Description | Type |
|---|---|---|
list | Order detail list | OrderDetail[] |
count | Total count | number |
pageIndex | Current page index | number |
pageSize | Page size | number |
OrderDetail Property
Note: At runtime
OrderDetailandOrderLineare converted viaHelpers.ToKscriptModel, so all date/time properties are serialized datestringvalues rather than JSDateobjects.
| Property | Description | Type |
|---|---|---|
id | Order ID | string |
customer | Customer information | Customer |
pointsDeductionAmount | Points deduction amount | number |
earnPoints | Points earned | number |
redeemPoints | Points redeemed | number |
totalAmount | Total order amount | number |
taxAmount | Tax amount | number |
originalAmount | Original amount | number |
shippingAmount | Shipping amount | number |
insuranceAmount | Insurance amount | number | null |
subtotalAmount | Subtotal amount | number |
originalSubtotalAmount | Original subtotal amount | number |
totalQuantity | Total quantity | number |
currency | Currency code | string |
paid | Whether paid | boolean |
paidAt | Payment time | string | null |
paymentMethod | Payment method | string |
delivered | Whether delivered | boolean |
partialDelivered | Whether partially delivered | boolean |
trackingNumber | Tracking number (unassigned in OrderDetail constructor, always null; read from order.lines[].trackingNumber) | string | null |
shippingCarrier | Shipping carrier (unassigned in OrderDetail constructor, always null; read from order.lines[].shippingCarrier) | string | null |
shippingAt | Shipping time | string | null |
scheduledDeliveryTime | Estimated delivery time | string | null |
canceled | Whether canceled | boolean |
cancelReason | Reason for cancellation | string |
cancelAt | Cancellation time | string | null |
createdAt | Creation time | string |
updatedAt | Update time | string |
shippingAddress | Shipping address | Address |
note | Order note | string |
lines | Order items | OrderLine[] |
ip | Client IP address | string |
country | Country | string |
source | Order source | string |
clientInfo | Client information | ClientInfo |
extensionButton | Extension button | { text: string, url: string } |
discountAllocations | Automatic discount allocations | DiscountAllocation[] |
shippingAllocations | Shipping allocations | ShippingAllocation[] |
extensionFields | Extension fields | KeyValue[] |
CreateOrderLineInit Property
| Property | Description | Type |
|---|---|---|
title | Product title | string |
price | Unit price | number |
quantity | Quantity | number |
image | Product image URL | string |
extensionButton | Extension button | { text: string, url: string } |
Customer attribute
| Property | Description | Type |
|---|---|---|
id | Customer ID | string |
email | Customer email | string |
firstName | Customer first name | string |
lastName | Customer last name | string |
phone | Customer phone number | string |
Address property
| Property | Description | Type |
|---|---|---|
id | Address ID | string |
isDefault | Whether default address | boolean |
country | Country | string |
province | Province / state | string |
city | City | string |
address1 | Address line 1 | string |
address2 | Address line 2 | string |
firstName | Recipient first name | string |
lastName | Recipient last name | string |
phone | Phone number | string |
zip | Postal code | string |
OrderLine Property
| Property | Description | Type |
|---|---|---|
id | Order line ID | string |
totalAmount | Line total amount | number |
taxAmount | Line tax amount | number |
originalAmount | Line original amount | number |
quantity | Line quantity | number |
totalQuantity | Total quantity of items ordered | number |
price | Line unit price | number |
originalPrice | Original line unit price | number |
title | Order item title | string |
image | Order item image URL | string |
productId | Product ID | string |
variantId | Variant ID | string |
sku | Product SKU | string |
orderId | Order ID | string |
options | Product options | { name: string, value: string }[] |
discountAllocations | Automatic discount allocations | DiscountAllocation[] |
groupName | Bundle group name | string |
isMain | Whether main bundle product | boolean |
note | Order line note | string |
extensionButton | Extension button | { text: string, url: string } |
trackingNumber | Tracking number | string |
shippingCarrier | Shipping carrier | string |
shippingAt | Shipping time | string | null |
delivered | Whether delivered | boolean |
digitalItems | Digital product items | DigitalOrderItem[] |
autoDelivery | Whether delivered automatically | boolean |
isDigital | Whether digital product | boolean |
maxDownloadCount | Maximum download count | number | null |
maxDownloadDay | Maximum download days | number | null |
errorMessage | Error message | string |
ClientInfo Property
| Property | Description | Type |
|---|---|---|
platform | Platform | string |
oS | Operating system | string |
device | Device | string |
application | Application information | ApplicationInfo |
ApplicationInfo Property
| Property | Description | Type |
|---|---|---|
isWebBrowser | Whether web browser | boolean |
name | Application name | string |
version | Application version | string |
TypeScript Definitions
Show full TypeScript definitions
interface KOrder {
create(cartId: string, options: KScript.Commerce.Models.CreateOrderOptions): Kooboo.Sites.Commerce.ViewModels.OrderDetail;
create(init: KScript.Commerce.Models.CreateOrderInit): Kooboo.Sites.Commerce.ViewModels.OrderDetail;
get(orderId: string): Kooboo.Sites.Commerce.ViewModels.OrderDetail;
pay(orderId: string, method: string): void;
delivery(orderId: string, shippingCarrier: string, trackingNumber: string): void;
delivery(orderId: string, lineIds: string[]): void;
updateShippingInfo(orderId: string, orderLineId: string, options: Kooboo.Sites.Commerce.ViewModels.DeliveryOptions): void;
cancel(orderId: string, reason: string): void;
reopen(orderId: string): void;
updateAddress(orderId: string, address: Kooboo.Sites.Commerce.Entities.Address): void;
list(query?: KScript.Commerce.Models.OrderQuery): Kooboo.Sites.Commerce.ViewModels.OrderDetail[];
delete(id: string): void;
updateExtensionField(id: string, property: string, value: string): void;
}
interface OrderQuery extends CustomerOrderQuery {
customerId: string;
paid?: boolean;
delivered?: boolean;
canceled?: boolean;
startDate?: Date;
endDate?: Date;
pageIndex: number;
pageSize: number;
}
interface OrderResult {
list: Kooboo.Sites.Commerce.ViewModels.OrderDetail[];
count: number;
pageIndex: number;
pageSize: number;
}
interface CreateOrderOptions {
scheduledDeliveryTime?: Date;
note: string;
address: Kooboo.Sites.Commerce.Entities.Address;
extensionFields: KScript.KeyValue[];
currency: string;
}
interface CreateOrderInit {
note: string;
source: string;
address: Kooboo.Sites.Commerce.Entities.Address;
shippingAmount: number;
customerId: string;
email: string;
firstName: string;
lastName: string;
phone: string;
extensionButton: Kooboo.Sites.Commerce.Entities.ExtensionButton;
extensionFields: KScript.KeyValue[];
lines: CreateOrderLineInit[];
currency: string;
}
interface CreateOrderLineInit {
image: string;
price: number;
quantity: number;
title: string;
extensionButton: Kooboo.Sites.Commerce.Entities.ExtensionButton;
}
interface DeliveryOptions {
shippingCarrier: string;
trackingNumber: string;
digitalItems: Kooboo.Sites.Commerce.Entities.DigitalOrderItem[];
}
interface OrderDetail {
id: string;
customer: CustomerInfo;
pointsDeductionAmount: number;
earnPoints: number;
redeemPoints: number;
totalAmount: number;
taxAmount: number;
originalAmount: number;
shippingAmount: number;
insuranceAmount?: number;
subtotalAmount: number;
originalSubtotalAmount: number;
totalQuantity: number;
currency: string;
paid: boolean;
paidAt?: string; // string | null at runtime via ToKscriptModel
paymentMethod: string;
delivered: boolean;
partialDelivered: boolean;
trackingNumber: string | null; // unassigned (null) at runtime; read from lines[].trackingNumber
shippingCarrier: string | null; // unassigned (null) at runtime; read from lines[].shippingCarrier
shippingAt?: string;
scheduledDeliveryTime?: string;
canceled: boolean;
cancelReason: string;
cancelAt?: string;
createdAt: string;
updatedAt: string;
shippingAddress: Kooboo.Sites.Commerce.Entities.Address;
note: string;
lines: Kooboo.Sites.Commerce.Entities.OrderLine[];
ip: string;
country: string;
source: string;
clientInfo: Kooboo.Lib.Utilities.UAParser.ClientInfo;
extensionButton: Kooboo.Sites.Commerce.Entities.ExtensionButton;
discountAllocations: Kooboo.Sites.Commerce.Calculate.DiscountAllocation[];
shippingAllocations: Kooboo.Sites.Commerce.Calculate.ShippingAllocation[];
extensionFields: KScript.KeyValue[];
}Related Docs
- k.commerce - E-commerce module overview
- cart - Shopping cart
- product - Product management
- customer - Customer management