Drupal Commerce. Working with currencies, prices, price formats

Difficulty level: 

Drupal Commerce works with different currencies, the field Price is customizable and can be displayed without decimals, with a prefix or suffix, can be struck-through by the action. More in detail.
Article is translated from russian by Google Translate and corrected by tlito.

Translation field Price, Order Total, see the tutorial (in russian) "Drupal Commerce Basic setup".

Multi currency store Drupal Commerce

After installing multicurrency Drupal will automatically load the current exchange rate for each currency (regularly), so set the rates manually is not necessary, although it is possible. Multicurrency functions implemented with Commerce Multicurrency, which allows the use of multiple currencies and convert prices in the currency in accordance with the current rate.

Currency settings are performed: Shop - Configuration - Settings currency: /admin/commerce/config/currency

Commerce Multicurrency module adds two block select Currency: drop-down list (Currency Selector), or link menu (Currency Menu Selector).

Sometimes, after installing multicurrency if you choose another currency store (in the selection), the prices for the hryvnia, Belarusian rubles does not change and remains at that number, which is set in the price to another currency. This can happen if you do not set up the currency Shop - Configuration - Setting currency and if Drupal could not upload rates.

Problem: Exchange rates hryvnia, Belarusian ruble and the other is not always loaded because the default Drupal uses a source of exchange rates European Central Bank.
Solution: For startup of exchange rates for the hryvnia, Belarusian rubles, tenge (and other CIS countries) to use instead of the European Central Bank source current exchange rates - The Central Bank of Russia (CRB) - it is a module Commerce Multicurrency provider for RUB.
Selecting CRH after the installation of the module is made here: /admin/commerce/config/currency/conversion

Set different prices for different currencies

If you do not need some of the goods equivalent in another currency, and the need to set different prices for currencies, then you need to adjust the price of a commercial product field to enter multiple values.
It is necessary, for example, if European prices in euros differ from the prices for the Russian ruble.
Videotutorial English: https://commerceguys.com/blog/commerce-module-tuesday-commerce-multicurr...

Advanced Settings Currency

Commerce Currency Settings - module, which adds more options for currencies: the number of decimal places, the currency icon, display the currency symbol before or after price (suffix or prefix) , thousands separator, the decimal parts and other settings.
Currency setting the module located in the store (Commerce) - Configuration - Settings Currency - Setting currency (Custom Currence settings), it is here / admin / commerce / config / currency / custom-settings
This module allows you to remove the display of numbers after the decimal point, as you can not specify 0 characters in the Decimals (decimal). Signs for rubles may appear on the conversion price set in another currency into rubles. To erase decimal places, use the suggestions below.

Remove the penny for rubles, hryvnia and Belarusian rubles, but to leave the euro and the dollar

Remove the number after the decimal point for rubles, hryvnia in the prices of goods for the single currency basket shop - module Commerce Extra Price Formatters, and for multi-store - its hook.

  1. Module Commerce Extra Price Foramtters adds a display format for the field Price. After installing the module, open the display manager for all types of goods in the price list the format Price with no decimals, as well as for all fields views. - The problem is that if your multi-store, not all currencies have to clean up a penny, and the module removes a penny for all currencies with no options.

  2. hook_commerce_currency_info_alter() will hide decimal digits for the certain currencies.

The following code removes digits after point for three currencies: RUB - Russian Rubles, UAH - hryvnia, BYR - Belarussian rubles. You can remove unwanted line with currencies RUB, UAH, BYR or add a similar line with the codes of other currencies, such as KZT - for tenge, CZT - Czech Koruna, LTL - Lithuanian Litas, LV - Latvian Lat, and so on. For other currencies, that are not presented in this hook, the module is not affected by, the prices will be displayed with an equity component or as you have set in the Display Manager.

  1. /**
  2.  * Implements hook_commerce_currency_info_alter().
  3.  */
  4. function commerce_my_price_decimals_formatter_commerce_currency_info_alter(&$currencies, $langcode) {
  5. $currencies['RUB']['format_callback'] = 'commerce_my_price_decimals_formatter_currency_format';
  6. $currencies['UAH']['format_callback'] = 'commerce_my_price_decimals_formatter_currency_format';
  7. $currencies['BYR']['format_callback'] = 'commerce_my_price_decimals_formatter_currency_format';
  8. }
  10. /**
  11.  * Currency format callback.
  12.  */
  13. function commerce_my_price_decimals_formatter_currency_format($amount, $currency, $object) {
  14. $round_amount = commerce_currency_round(abs($amount), $currency);
  15. $price = number_format($round_amount, 0, $currency['decimal_separator'], $currency['thousands_separator']);
  17. $replacements = array(
  18. '@code_before' => $currency['code_placement'] == 'before' ? $currency['code'] : '',
  19. '@symbol_before' => $currency['symbol_placement'] == 'before' ? $currency['symbol'] : '',
  20. '@price' => $price,
  21. '@symbol_after' => $currency['symbol_placement'] == 'after' ? $currency['symbol'] : '',
  22. '@code_after' => $currency['code_placement'] == 'after' ? $currency['code'] : '',
  23. '@negative' => $amount < 0 ? '-' : '',
  24. '@symbol_spacer' => $currency['symbol_spacer'],
  25. '@code_spacer' => $currency['code_spacer'],
  26. );
  28. return trim(t('@code_before@code_spacer@negative@symbol_before@price@symbol_spacer@symbol_after@code_spacer@code_after', $replacements));
  29. }