Magento – Mage::app()->getLocale()->currency(‘AED’)->getSymbol() return NULL in magento 1.9

currencymagento-1.9

I have created multi currency website in Magento 1.9. On product page I'm trying to get current currency symbol using below code :

$symbol = Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol();

This code is working for all currency except AED. For AED currency above code return NULL.

Why this is happening?
How to resolve it?

Best Answer

It seems like Magento does not have Symbols for some of the currencies. You need to add symbols for these currencies manually.

To add currency symbol, Go to System > Manage Currencies > Symbol

  • Uncheck "use standard" for your currency (AED (United Arab Emirates Dirham))
  • Add symbol you want to add for AED
  • Click on Save Currency Symbols

PS: There are some more currencies which does not have default symbols available in Magento. e.g. SEK, SDG, GQE

EDIT

I have debug default Magento Currency display functionality and I found below. When Magento displays product price, It calls function

Mage_Directory_Model_Currency::formatTxt($price, $options = array())

This function then returns below

return Mage::app()->getLocale()->currency($this->getCode())->toCurrency($price, $options);

Here toCurrency function is called from lib/Zend/Currency.php

Zend_Currency::toCurrency($value = null, array $options = array())

For AED currency it sends one parameter in $options['display'] with value 3 If you var_dump($options) in this function you will get all data like below.

array (size=12)
  'precision' => int 2
  'position' => int 8
  'script' => null
  'format' => null
  'display' => int 3
  'name' => string 'United Arab Emirates Dirham' (length=27)
  'currency' => string 'AED' (length=3)
  'symbol' => null
  'locale' => string 'en_US' (length=5)
  'value' => int 0
  'service' => null
  'tag' => string 'Zend_Locale' (length=11)

In the same function there is below code.

switch($options['display']) {
    case self::USE_SYMBOL:
        $sign = $this->_extractPattern($options['symbol'], $original);
        break;

    case self::USE_SHORTNAME:
        $sign = $options['currency'];
        break;

    case self::USE_NAME:
        $sign = $options['name'];
        break;

    default:
        $sign = '';
        $value = str_replace(' ', '', $value);
        break;
}

This code defines what symbol, string to append for price. For AED it uses USE_SHORTNAME, and thus it adds currency short name and not symbol.

That is the reason you see Currency symbol (which is actually currency short name) in price and not in your code

Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol();

As you can see in var_dump data the symbol value for AED is null

Related Topic