Magento: How to change Currency symbol by Model Override ?

In my previous article, I had written about how you can change the currency symbol by making some change in the Zend (lib/Zend/Locale/Data/en.xml) file.

It’s easy way but the main drawback of this method is that all your changes will vanish once you upgrade Magento.

A better way will be overriding Magento model classes that are responsible for fetching the currency symbol.

This article shows how to override Magento model classes to change the currency symbol.

Scenario

I need to change the currency symbol of Nepalese Rupee (from Nrs to Rs). By default, the currency symbol for Nepalese Rupee is Nrs.

Solution

Create a new module and Override two model classes.

1) Mage_Core_Model_Locale_Currency
2) Mage_Core_Model_Locale

Config file of my module
(MyNamespace/MyModule/etc/config.xml)


<global>
	<models>		
		<core>
			<rewrite>			
				<locale_currency>MyNamespace_MyModule_Model_Locale_Currency</locale_currency>
				<locale>MyNamespace_MyModule_Model_Locale</locale>
			</rewrite>
		</core>		
	</models>
</global>

MyNamespace_MyModule_Model_Locale_Currency class of my module
(MyNamespace/MyModule/Model/Locale/Currency.php)

In this class I have changed currency symbol from Nrs to Rs.


class MyNamespace_MyModule_Model_Locale_Currency extends Mage_Core_Model_Locale_Currency
{
	/**
     * Creates a currency instance. Every supressed parameter is used from the actual or the given locale.
     *
     * @param  string             $currency OPTIONAL currency short name
     * @param  string|Zend_Locale $locale   OPTIONAL locale name
     * @throws Zend_Currency_Exception When currency is invalid
     */
    public function __construct($currency = null, $locale = null)
    { 		
		if (Zend_Locale::isLocale($currency, true, false)) {
            $temp     = $locale;
            $locale   = $currency;
            $currency = $temp;
        }
		
        $this->setLocale($locale);
		
        // Get currency details
        $this->_options['currency'] = Zend_Currency::getShortName($currency, $this->_locale);
        $this->_options['name']     = Zend_Currency::getName($currency, $this->_locale);
		//$this->_options['symbol']   = Zend_Currency::getSymbol($currency, $this->_locale);
		
		/************************************************
		 * Here's the logic to change the currency symbol
		 ************************************************
		 */
		if(Zend_Currency::getSymbol($currency, $this->_locale) == 'Nrs') {
			$this->_options['symbol'] = 'Rs';
		}
		else {
		    $this->_options['symbol']   = Zend_Currency::getSymbol($currency, $this->_locale);
		}
		
        if (($this->_options['currency'] === null) and ($this->_options['name'] === null)) {
            #require_once 'Zend/Currency/Exception.php';
            throw new Zend_Currency_Exception("Currency '$currency' not found");
        }
		
        // Get the format
        $this->_options['position'] = $this->_updateFormat();
        $this->_options['display']  = Zend_Currency::NO_SYMBOL;
        if (empty($this->_options['symbol']) === false) {
            $this->_options['display'] = Zend_Currency::USE_SYMBOL;
        } else if (empty($this->_options['currency']) === false) {
            $this->_options['display'] = Zend_Currency::USE_SHORTNAME;
        }
		
        //parent::__construct($currency, $locale);
        $this->_options['symbol_choice'] = self::getSymbolChoice($currency, $this->_locale);	
    }
}

MyNamespace_MyModule_Model_Locale class of my module
(MyNamespace/MyModule/Model/Locale.php)


class MyNamespace_MyModule_Model_Locale extends Mage_Core_Model_Locale
{	
	/**
	* Create Mage_Core_Model_Locale_Currency object for current locale
	*
	* @param   string $currency
	* @return  Mage_Core_Model_Locale_Currency
	*/
    public function currency($currency)
    { 
        Varien_Profiler::start('locale/currency');
        if (!isset(self::$_currencyCache[$this->getLocaleCode()][$currency])) {
            try {				
                $currencyObject = new MyNamespace_MyModule_Model_Locale_Currency($currency, $this->getLocale());
				
            } catch (Exception $e) {				
                $currencyObject = new MyNamespace_MyModule_Model_Locale_Currency($this->getCurrency(), $this->getLocale());
                $options = array(
                        'name'      => $currency,
                        'currency'  => $currency,
                        'symbol'    => $currency
                );
                $currencyObject->setFormat($options);
            }
            self::$_currencyCache[$this->getLocaleCode()][$currency] = $currencyObject;
        }
        Varien_Profiler::stop('locale/currency');
        return self::$_currencyCache[$this->getLocaleCode()][$currency];
    }	
}

In this way, you can override Magento Model classes to change the currency symbol.

Hope this helps. Thanks.