Skip to content

k.commerce.currency ​

Currency management.

Execution ContextPrerequisitesTypical Use
KScript API (k.api.get / k.api.post), server-side <script env="server">Configure store currency and exchange rates in commerce settingsDisplay currency switchers, read default currency code, and convert prices

k.commerce.currency provides currency list lookups and exchange-rate conversions.

Quick Start ​

js
k.api.get(() => {
    const defaultCode = k.commerce.currency.default
    const currencies = k.commerce.currency.list()
    const converted = k.commerce.currency.convert("USD", "CNY", 2)
    return { defaultCode, currencies, converted }
})

Quick Reference ​

Method / PropertyDescriptionReturns
defaultStore default currency code (e.g. "CNY")string
list()List configured currencies and exchange ratesCurrencyItem[]
convert(from, to, value)Convert an amount between currencies (uses site manual rate only when from is default currency and to has auto-update disabled; otherwise queries remote rate service)number

default ​

Read-only property returning the store default currency code. No parameters.

Returns: string

js
k.api.get(() => {
    return k.commerce.currency.default
})

list() ​

Get a list of supported currencies. No parameters.

Returns: CurrencyItem[]

js
k.api.get(() => {
    return k.commerce.currency.list()
})

convert() ​

Convert an amount between two currency codes.

Rate Resolution & Exceptions

  • Site manual rate: A store-configured manual exchangeRate is used only when from matches the store default currency (case-insensitive) and to is configured in the store with AutoUpdate disabled.
  • Remote rate service: In all other cases (including conversions from a non-default currency, unconfigured target currencies, or currencies with AutoUpdate enabled), Kooboo queries the remote Kooboo account server and caches rates locally for 3 days (exchangeRates.json, refreshed in the background after expiry).
  • Errors: Passing empty/whitespace currency codes or failing to fetch from the remote rate service throws an exception (when from equals to, rate 1 is used directly).
ParameterTypeRequiredDescription
fromstringYesSource currency code
tostringYesTarget currency code
valuenumberYesAmount to convert

Returns: number - Converted amount

js
k.api.get(() => {
    const usdAmount = 2
    const cnyAmount = k.commerce.currency.convert("USD", "CNY", usdAmount)
    return { usdAmount, cnyAmount }
})

Structure ​

CurrencyItem Property ​

PropertyDescriptionType
nameCurrency namestring
nameNativeLocal currency name (mirrors English name for default currency; currently null for non-default currencies)string | null
codeCurrency codestring
symbolCurrency symbolstring
exchangeRateExchange ratenumber

TypeScript Definitions ​

ts
interface KCurrency {
    default: string;
    list(): CurrencyItem[];
    convert(from: string, to: string, value: number): number;
}

interface CurrencyItem {
    name: string;
    nameNative: string | null;
    code: string;
    symbol: string;
    exchangeRate: number;
}