Skip to content

currency_chooser

The currency_chooser macro is used to render a group of radio boxes for currencies available in the shop with additional title. It is a part of language_and_currency module but can also be used alone to give more control over how the module is rendered if needed.

Definition

{% currency_chooser(currencies, options) %}

Input parameters

currencies

object represents an object of parameters related to currencies

Option key Type Default Required Description
options.current Currency null yes Currency object of currently active currency in the shop
options.list CurrenciesList null yes CurrenciesList object of currently available currencies in the shop

options

object represents an object of currency chooser options

Option key Type Default Required Description
options.instanceId string "" yes unique identifier - commonly provided by special module variable

Example

In this example we render a basic currency chooser. To get a list of currencies as well as a currently chosen currency we use Object Api methods getCurrencies() and getShopCurrency().

{% from "@macros/currency_chooser.twig" import currency_chooser %}

{% set currencies = ObjectApi.getCurrencies() %}
{% set shopCurrency = ObjectApi.getShopCurrency() %}

{{
    currency_chooser({
        current: shopCurrency,
        list: currencies
    }, {
        instanceId: moduleInstance
    })
}}

Macro source code

{% macro currency_chooser(currencies, options) %}
    {% from "@macros/radio_box.twig" import radio_box %}

    <p class="font_size-s">{{ translate('Currency') }}</p>

    <div class="radio-box-group">
        {% for currency in currencies.list %}
            {% set labelSymbol = currency.symbol ? "(#{currency.symbol})" : '' %}

            {{
                radio_box({
                    id: "currency-radio-box-#{options.instanceId}-#{currency.id}",
                    name: "language-and-currency-currency-#{options.instanceId}",
                    value: currency.code,
                    label: "#{currency.shortName} - #{currency.name} #{labelSymbol}",
                    checked: currency.code == currencies.current.code,
                    classNames: 'radio-box-group__radio-box'
                })
            }}
        {% endfor %}
    </div>
{% endmacro %}

ObjectApi methods reference

Webcomponents reference