Frontend API
Methods to format money, and handle currency selection and conversion when multi-currency is enabled.
Returns a formatted string based on store currency settings.
This method will automatically use the store's base currency settings by default, or the selected currency in a multi-currency setup.
swell.currency.format(10) // => $10.00
Optionally override default currency settings:
swell.currency.format(10, {
code: 'EUR',
locale: 'en-GB',
decimals: 4, // number of decimal digits
rate: 0.8874, // conversion rate
convert: true // set to false to format the amount as-is
}) // => €8.8740
Returns an array of enabled currencies. Result will be empty if multi-currency has not been configured on the store.
await swell.currency.list()
[
{
"code": "USD",
"rate": 1,
"name": "US Dollar",
"symbol": "$",
"decimals": 2,
"type": "base"
},
{
"code": "EUR",
"rate": 0.82542,
"name": "Euro",
"symbol": "€",
"decimals": 2,
"type": "display"
},
{
"code": "AUD",
"rate": 1.28934,
"name": "Australian Dollar",
"symbol": "A$",
"decimals": 2,
"type": "display"
}
]
Sets a browser cookie and updates the user's session and cart to the selected currency.
In a multi-currency setup, call the select() method to change settings used by format() to render money values.
If a cart exists in the user's session, this method also updates the cart to reflect the user's currency preference.
await swell.currency.select('EUR')
Returns the selected currency code. Defaults to the store's base currency.
swell.currency.selected() // => USD
In a multi-currency setup, format() uses the options of the selected currency. Each enabled currency has a type, and the type determines whether the amount is converted.
- base: the store's base currency. Amounts are formatted without conversion.
- priced: a currency with prices entered directly against it. Amounts are formatted without conversion.
- display: a currency derived from the base currency. Amounts are multiplied by a conversion rate that is updated daily, then rounded using the currency's rounding settings.
Passing an explicit rate converts the amount for any currency type, and convert: false formats the amount as-is even for a display currency. An amount is also left unconverted when no rate is available, which is the case for a code that isn't one of the store's enabled currencies.
swell.currency.format(10) // => $10.00
await swell.currency.select('EUR')
swell.currency.format(10) // => €8.874
See our help center for more details on multi-currency setup.
Methods to format money, and handle currency selection and conversion when multi-currency is enabled.
List enabled currencies
Returns an array of enabled currencies. The result will be empty if multi-currency has not been configured on the store.
The parameters for this query are _locale and _currency.
query getStoreCurrencies {
storeSettings(_currency: "USD") {
store {
currencies {
code
name
rate
symbol
decimals
priced
type
}
}
}
}