Skip to content

Visitor Currency & Formatting

Currency handles both storefront visitor preference persistence and locale-aware monetary display formatting.

Visitor Currency Resolution (VisitorCurrencyResolver)

Section titled “Visitor Currency Resolution (VisitorCurrencyResolver)”

VisitorCurrencyResolver resolves the active storefront currency in strict priority order:

  1. Session ($session->setFor('Currency', ...)): Checked first for immediate consistency during the current request lifecycle.
  2. Cookie (pw_currency by default): Read from $input->cookie when allowVisitorOverride is enabled in Settings, written as HttpOnly; SameSite=Lax with the configured visitorCookieDays TTL (1..365).
  3. Base Currency Fallback: Falls back cleanly to $currency->baseCurrency()->code if no override is set or if the requested code is inactive.
<?php namespace ProcessWire;
$currency = Currency::requireInstance($wire);
// Read active storefront currency code (e.g. 'EUR' or visitor's 'USD')
$active = $currency->activeCurrency();
// Persist a validated active currency into session + HttpOnly cookie
$currency->setActiveCurrency('USD');
// Reset visitor override back to site base currency
$currency->visitor()->clearActiveCurrency();
// Populate storefront switcher dropdowns cleanly
$allowed = $currency->resolveAllowedCurrencies(['EUR', 'USD', 'GBP', 'TRY'], activeOnly: true);
$options = $currency->currencyOptions($allowed, activeOnly: true);

MoneyFormatter uses PHP’s ext-intl NumberFormatter inside exact float-safe integer bounds (999_999_999_999_999 minor units when minorUnits > 0, or 9_007_199_254_740_992 when minorUnits === 0) and automatically falls back to a PHP_INT_MIN-safe, locale-aware string formatter for extreme values without abs() overflow.

<?php namespace ProcessWire;
use Totoglu\Currency\Value\Money;
$currency = Currency::requireInstance($wire);
$price = Money::fromMajor('14700.00', 'EUR');
// Standard locale formatting
echo $currency->format($price, 'en_US'); // €14,700.00
echo $currency->format($price, 'de_DE'); // 14.700,00 €
// Accounting style for negative amounts: ($4,900.00)
echo $currency->format(Money::fromMajor('-4900.00', 'USD'), 'en_US', ['accounting' => true]);
// Show ISO code or override symbol
echo $currency->format($price, 'en_US', ['show_code' => true, 'show_symbol' => false]);
echo $currency->format($price, 'en_US', ['symbol_override' => 'EUR ']);

Next steps