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.

Format currency
swell.currency.format(10) // => $10.00

Optionally override default currency settings:

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.

List enabled currencies
await swell.currency.list()

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.

Select a currency
await swell.currency.select('EUR')

Returns the selected currency code. Defaults to the store's base currency.

Get the selected 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.

Multi-currency formatting
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.