k.commerce.loyalty
Member points and loyalty management.
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
KScript API (k.api.get / k.api.post), server-side <script env="server"> | Customer record exists; create membership tiers manually in Kooboo backend under E-commerce -> Member (hidden directory by default) | Check customer points and active membership tier, grant/redeem points, upgrade or renew membership, and paginate point history |
k.commerce.loyalty provides customer loyalty operations for reading membership status and point balance, granting or redeeming points, switching or renewing membership tiers, and querying point logs.
Member Creation
Membership tiers must be created manually in the Kooboo backend: E-commerce -> Member (in the hidden directory by default) -> Create member.
Quick Start
k.api.post(() => {
const customer = k.commerce.customer.getByEmail('test@example.com')
if (!customer) return null
k.commerce.loyalty.earnPoints(customer.id, 100, 'Login reward')
const status = k.commerce.loyalty.info(customer.id)
const logs = k.commerce.loyalty.points(customer.id, { pageIndex: 1, pageSize: 30 })
return {
points: status ? status.points : 0,
membership: status ? status.membershipStatus : null,
totalLogs: logs.count
}
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
info(customerId) | Get customer point balance and current membership status (also evaluates auto-upgrade rules) | LoyaltyInfo | null |
memberships() | List configured membership tiers | Membership[] |
changeMembership(customerId, membershipId) | Switch a customer to a different membership tier | void |
renewMembership(customerId) | Renew the customer's current membership tier | void |
earnPoints(customerId, points, description?) | Grant loyalty points to a customer | void |
redeemPoints(customerId, points, description?) | Redeem/deduct loyalty points from a customer | void |
points(customerId, query?) | Query paginated loyalty point ledger entries | LoyaltyPointResult |
info()
Get the customer's loyalty points and membership status. Internally calls MembershipService.Match() to evaluate and apply eligible auto-upgrade rules (allowAutoUpgrade), and returns null if the customer does not exist.
| Parameter | Type | Required | Description |
|---|---|---|---|
customerId | string | Yes | Customer ID |
Returns: LoyaltyInfo | null (returns null when the customer does not exist).
k.api.get(() => {
const customer = k.commerce.customer.getByEmail('test@example.com')
if (!customer) return null
const info = k.commerce.loyalty.info(customer.id)
if (!info) return null
return { points: info.points, membershipStatus: info.membershipStatus }
})changeMembership()
Change a customer's membership tier.
| Parameter | Type | Required | Description |
|---|---|---|---|
customerId | string | Yes | Customer ID |
membershipId | string | Yes | Membership ID |
Returns: void.
k.api.post(() => {
const customer = k.commerce.customer.getByEmail('test@example.com')
const memberships = k.commerce.membership.list()
if (memberships && memberships.length > 0) {
k.commerce.loyalty.changeMembership(customer.id, memberships[0].id)
}
return k.commerce.loyalty.info(customer.id)
})earnPoints()
Grant loyalty points to a customer.
| Parameter | Type | Required | Description |
|---|---|---|---|
customerId | string | Yes | Customer ID |
points | number | Yes | Number of points earned |
description | string | No | Points description |
Returns: void.
k.api.post(() => {
const customer = k.commerce.customer.getByEmail('test@example.com')
k.commerce.loyalty.earnPoints(customer.id, 100, 'Purchase reward')
return k.commerce.loyalty.info(customer.id)
})redeemPoints()
Redeem loyalty points from a customer.
| Parameter | Type | Required | Description |
|---|---|---|---|
customerId | string | Yes | Customer ID |
points | number | Yes | Amount of points redeemed |
description | string | No | Points description |
Returns: void.
k.api.post(() => {
const customer = k.commerce.customer.getByEmail('test@example.com')
k.commerce.loyalty.redeemPoints(customer.id, 50, 'Points redemption')
return k.commerce.loyalty.info(customer.id)
})points()
Get a paginated list of the customer's point history records.
| Parameter | Type | Required | Description |
|---|---|---|---|
customerId | string | Yes | Customer ID |
query | PagingQuery | No | Pagination parameters |
query.pageIndex | number | No | Page number, default 1 |
query.pageSize | number | No | Number of items per page, default 30 |
Returns: LoyaltyPointResult. Contains list (CustomerPointModel[]), count, pageIndex, and pageSize.
k.api.get(() => {
const customer = k.commerce.customer.getByEmail('test@example.com')
return k.commerce.loyalty.points(customer.id, {
pageIndex: 1,
pageSize: 20
})
})memberships()
Get the list of membership tiers. No parameters.
Returns: Membership[].
k.api.get(() => {
const memberships = k.commerce.loyalty.memberships()
return { count: memberships.length, items: memberships }
})renewMembership()
Renew a customer's current membership tier.
| Parameter | Type | Required | Description |
|---|---|---|---|
customerId | string | Yes | Customer ID |
Returns: void.
k.api.post(() => {
const customer = k.commerce.customer.getByEmail('test@example.com')
k.commerce.loyalty.renewMembership(customer.id)
return k.commerce.loyalty.info(customer.id)
})Structures
LoyaltyInfo Property
| Property | Description | Type |
|---|---|---|
points | Member points | number |
membershipStatus | Member status (null when the customer has no active membership and matches no auto-upgrade rule) | MembershipStatus | null |
MembershipStatus Property
| Property | Description | Type |
|---|---|---|
membership | Member information | Membership |
startedAt | Membership start time | Date |
endAt | Membership end time | Date |
active | Whether membership is active | boolean |
Membership attribute
| Property | Description | Type |
|---|---|---|
id | Membership ID | string |
name | Membership name | string |
description | Membership description | string |
price | Membership price | number |
duration | Membership duration | number |
durationUnit | Duration unit | "Year" | "Month" | "Day" | "Week" |
priority | Membership priority | number |
condition | Upgrade conditions | Condition |
allowAutoUpgrade | Whether automatic upgrades are allowed | boolean |
allowPurchase | Whether direct purchase is allowed | boolean |
customData | Custom key-value data | KeyValue[] | null |
createdAt | Creation time (string from memberships(); Date via info().membershipStatus.membership) | string | Date |
updatedAt | Update time (string from memberships(); Date via info().membershipStatus.membership) | string | Date |
Note:
memberships()andpoints()pass entities throughHelpers.ToKscriptModel, which converts C#DateTimevalues intostring(matching membership). By contrast,info()returns the CLRLoyaltyInfoobject directly, somembershipStatus.startedAt/endAtandmembershipStatus.membership.createdAt/updatedAtare JSDateinstances at runtime.
Condition property
| Property | Description | Type |
|---|---|---|
isAny | Whether matching any condition suffices | boolean |
items | List of condition items | ConditionItem[] |
ConditionItem Property
| Property | Description | Type |
|---|---|---|
option | Condition option | string |
method | Condition operator | string |
value | Condition value | string |
LoyaltyPointResult Properties
| Property | Description | Type |
|---|---|---|
list | Point history items | CustomerPointModel[] |
count | Total count | number |
pageIndex | Current page index | number |
pageSize | Page size | number |
CustomerPointModel Property
| Property | Description | Type |
|---|---|---|
customerId | Customer ID | string |
points | Point change amount | number |
createdAt | Creation time (serialized to string at runtime via ToKscriptModel) | string |
type | Change type (OrderEarn / OrderRedeem / ManualEarn / ManualRedeem / LoginEarn) | string |
description | Description | string |
key | Tracking key | string |
disabled | Whether disabled | boolean |
TypeScript Definitions
Show full TypeScript definitions
interface KLoyalty {
memberships(): Kooboo.Sites.Commerce.Entities.Membership[];
changeMembership(customerId: string, membershipId: string): void;
renewMembership(customerId: string): void;
earnPoints(customerId: string, points: number, description?: string): void;
redeemPoints(customerId: string, points: number, description?: string): void;
points(customerId: string, query?: Kooboo.Sites.Commerce.ViewModels.PagingQuery): KScript.Commerce.Models.LoyaltyPointResult;
info(customerId: string): KScript.Commerce.Models.LoyaltyInfo | null;
}
interface LoyaltyInfo {
points: number;
membershipStatus: MembershipStatus | null;
}
interface MembershipStatus {
membership: Kooboo.Sites.Commerce.Entities.Membership;
startedAt: Date;
endAt: Date;
active: boolean;
}
interface Membership extends Kooboo.Sites.Commerce.Entity {
name: string;
description: string;
price: number;
durationUnit: Kooboo.Data.Models.DurationUnit;
duration: number;
priority: number;
condition: Kooboo.Sites.Commerce.Condition.Define;
allowPurchase: boolean;
allowAutoUpgrade: boolean;
customData: KScript.KeyValue[];
id: string;
createdAt: Date; // string when returned by memberships()
updatedAt: Date; // string when returned by memberships()
clone(): any;
getGuid(): any;
}
interface LoyaltyPointResult {
list: Kooboo.Sites.Commerce.DataStorage.CustomerPointModel[];
count: number;
pageIndex: number;
pageSize: number;
}
interface CustomerPointModel {
customerId: string;
points: number;
createdAt: string; // C# DateTime, serialized to string at runtime via ToKscriptModel
type: ChangeType;
description: string;
key: string;
disabled: boolean;
}
type ChangeType = 'OrderEarn' | 'OrderRedeem' | 'ManualEarn' | 'ManualRedeem' | 'LoginEarn';
interface PagingQuery {
pageIndex: number;
pageSize: number;
}Related Docs
- k.commerce - E-commerce module overview
- customer - Customer management
- membership - Membership tiers