Change currency format in Drupal Commerce

Posted on 14/08/2016

Defaults shipped with Drupal Commerce should work fine for most stores. However, exactly because of this, some of the details can be annoying and unnecessary for a specific project. The way currencies are formatted is one of those things.

By default, Commerce shows all values with the decimals separated by a period. For projects that need large amounts (e.g. high end jewelry or fashion pieces, aircraft parts, cars, etc.), the decimals have no place and it's better to remove them.

There are two ways to achieve this:

  1. Use the Commerce Currency Settings module.
  2. Implement hook_commerce_currency_info_alter() in your custom module.

Since the task is very simple and will most likely never need to be updated after initial setup, there is no need to install another module. This means less components to think about and update in the long run, and less pages in the admin.

By implementing this in your custom module you will also get complete freedom to change the format in which the values are displayed. You can replace the dollar sign with USD, move it before / after the value or add custom labels.

Here's the code you need to use:

  1. /**
  2.  * Implements hook_commerce_currency_info_alter().
  3.  */
  4. function YOUR_MODULE_commerce_currency_info_alter(&$currencies) {
  5.   // Get the abbreviation of the default currency. This will return "USD" for
  6.   // US Dollars, "EUR" for Euros, etc.
  7.   // If your site is using multiple currencies, you can skip this and just use
  8.   // the abbreviation of the currency you want to change the format for.
  9.   $default_currency = commerce_default_currency();
  10.   $currencies[$default_currency]['format_callback'] = 'YOUR_MODULE_commerce_currency_format';
  11. }
  12.  
  13. /**
  14.  * Commerce currency format callback.
  15.  */
  16. function YOUR_MODULE_commerce_currency_format($amount, $currency, $currency_code, $object = NULL, $convert = TRUE) {
  17.   // Format the price as a number.
  18.   $price = number_format(commerce_currency_round(abs($amount), $currency), 0, $currency['decimal_separator'], $currency['thousands_separator']);
  19.  
  20.   // Establish the replacement values to format this price for its currency.
  21.   $replacements = array(
  22.     '@code_before' => $currency['code_placement'] == 'before' ? $currency['code'] : '',
  23.     '@symbol_before' => $currency['symbol_placement'] == 'before' ? $currency['symbol'] : '',
  24.     '@price' => $price,
  25.     '@symbol_after' => $currency['symbol_placement'] == 'after' ? $currency['symbol'] : '',
  26.     '@code_after' => $currency['code_placement'] == 'after' ? $currency['code'] : '',
  27.     '@negative' => $amount < 0 ? '-' : '',
  28.     '@symbol_spacer' => $currency['symbol_spacer'],
  29.     '@code_spacer' => $currency['code_spacer'],
  30.   );
  31.  
  32.   return trim(t('@code_before@code_spacer@negative@symbol_before@price@symbol_spacer@symbol_after@code_spacer@code_after', $replacements));
  33. }

Do note: if you place the hook_commerce_currency_info_alter() implementation in YOUR_MODULE.commerce.inc file, the callback will still need to be placed in the .module file, because the .commerce.inc file will not be automatically loaded everywhere.

That's it - all prices will be displayed now without the decimals.

See also: