5

I know there must be an API that returns the current currency symbol, but when I searched for it, I didn't find anything.

How can I retrieve the current currency symbol?

4 Answers 4

3

With the Commerce Multicurrency module and Drupal 7, you can use the following code.

$enabled_curr = commerce_currencies(TRUE);
$current_curr = $enabled_curr[commerce_multicurrency_get_user_currency_code()];
$curr_symbol = $current_curr['symbol'];
3

With Drupal 7, if you want to get the default (current) currency you can use the following code.

$cur = commerce_currency_load();
$sym = $cur['symbol'];
0
3

From Drupal 8, you can use the following code.

// Add the following line on the top of the file using the rest of the code.
use Drupal\commerce_price\Entity\Currency;

// Get the currency code from the active store.
$currency_code = $store->get('default_currency')->getValue()[0]['target_id'];
$currency = Currency::load($currency_code);
$symbol = $currency->get('symbol');
1
  • Any specific reason not to use direct calls like $store->getDefaultCurrencyCode() or $currency->getSymbol()? Maybe these weren't available back then? They sure are now...
    – Gábor
    Commented Aug 25, 2022 at 19:16
2

With Drupal 8/9, if you are using the commerce_currency_resolver module (which is pretty cool):

$currency_code = \Drupal::service('commerce_currency_resolver.current_currency')
                 ->getCurrency();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.