Home » Magento

Magento: How to change Currency symbol by Model Override ?

20 July 2010 2,846 views Popularity: 6% Share/Bookmark

email

In my previous article, I have 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.

In this article, I will be showing you 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.

Related posts:

  1. Magento: How to change Currency symbol ?
  2. Magento: Convert Price from Current Currency to Base Currency and vice-versa
  3. Magento: Block Controller Model Helper Override
  4. Magento: How to get Currency Rates?
  5. Magento: Setup multiple currency shop
  6. Magento: Show Currency Selector in right sidebar
  7. Magento: Show Currency Selector in header
  8. Magento: Authorize.net not displayed in Payment Information section while Checkout
  9. Magento: How to change or reorder top links?
  10. Magento: PayPal Website Payments Standard not displayed
  • http://9 Patrick B.

    Hi Mukesh,

    I have Magento 1.4.1 and your module seem to be not working ?

    Have a idea ?

    Thanks

  • Saurabh

    Hi,

    How to integrate new rupee sumbol in magento,

    I tried the CSS way but it don’t work

    Thanks & Regards,
    Saurabh

  • Anatoliy

    Hi,

    it is new article, good approach, but it doesnt work. To get it running change your Locale.php to this:


    class MyNamespace_MyModul_Model_Locale extends Mage_Core_Model_Locale
    {

    /**
    * Create Zend_Currency object for current locale
    *
    * @param string $currency
    * @return Zend_Currency
    */
    public function currency($currency)
    {
    Varien_Profiler::start('locale/currency');
    if (!isset(self::$_currencyCache[$this->getLocaleCode()][$currency])) {
    try {
    $currencyObject = new Zend_Currency($currency, $this->getLocale());
    if ($currency == "UAH") {
    $currencyObject->setFormat(array('symbol'=> 'грн'));
    }
    } catch (Exception $e) {
    $currencyObject = new Zend_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];
    }
    }

    And config-file should look like this:

    Narechena_Zend_Model_Locale

    Regards,
    Anatoliy