Add A Magento Currency Exchange Update Service

back to tech articles
Magento CE 1.8.1.0

If you’re reading this, you probably know that the default currency exchange rate updater that ships with Magento Community Edition is broken. I was hoping this would have been addressed in 1.8, but alas, no. Anyway, we can hardly complain – they still offer the platform for free!!

So, we need to replace the default Webservicex with another, working exchange rate updater. There are several available; XE, Yahoo, ECB to mention just a few. I’ll show you how to use the Yahoo Finance one in this talk. Why the Yahoo one, you ask? Well, it’s reliable for one thing! Let’s go.

Feeling lazy? About to get fired? Grab the files on GitHub.

Custom Module

We’re going to create a custom module to extend the functionality of Magento’s core. Since you’re still reading, I’m going to assume that you know how to do that.

File: app/etc/modules/TC_Currency.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0"?>
<config>
    <modules>
        <TC_Currency>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Adminhtml />
                <Mage_Core />
            </depends>
        </TC_Currency>
    </modules>
</config>

The depends are not really necessary, but it’s not a bad idea to include them, just to be sure. Next, we’ll build the module. Don’t panic because it’s only 2 files.

File: app/code/local/TC/Currency/etc/config.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0"?>
<config>
    <modules>
        <TC_Currency>
            <version>1.0.0</version>
        </TC_Currency>
    </modules>
    <global>
        <currency>
            <import>
                <services>
                    <yahoofinance>
                        <name>Yahoo Finance</name>
                        <model>TC_Currency_Model_Yahoo</model>
                    </yahoofinance>
                </services>
            </import>
        </currency>
        <models>
            <tc_currency>
                <class>TC_Currency_Model_Yahoo</class>
            </tc_currency>
        </models>
    </global>
</config>

And lastly, we need to add a Model to drive all this and do the heavy lifting.

File: app/code/local/TC/Currency/Model/Yahoo.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
class TC_Currency_Model_Yahoo extends Mage_Directory_Model_Currency_Import_Abstract
{
    protected $_url = 'http://quote.yahoo.com/d/quotes.csv?s={{CURRENCY_FROM}}{{CURRENCY_TO}}=X&f=l1&e=.csv';
    protected $_messages = array();

    protected function _convert($currencyFrom, $currencyTo, $retry=0) {
        $url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, $this->_url);
        $url = str_replace('{{CURRENCY_TO}}', $currencyTo, $url);

        try {
            sleep(1);
            $handle = fopen($url, "r");
            $exchange_rate = fread($handle, 2000);
            fclose($handle);

            if (!$exchange_rate) {
                $this->_messages[] = Mage::helper('directory')->__('Cannot retrieve rate from %s', $url);
                return null;
            }

            return (float) $exchange_rate * 1.0;
        } catch (Exception $e) {
            if ($retry == 0) {
                $this->_convert($currencyFrom, $currencyTo, 1);
            } else {
                $this->_messages[] = Mage::helper('directory')->__('Cannot retrieve rate from %s', $url);
            }
        }
    }
}

By default, the Webservicex model sits at a deeper level than this, we’re just keeping the structure simple.

Conclusion

Our module is finished. You’ll need to flush the caches once this has been uploaded to your Magento installation. The currency update service appears in the following select lists:

  • Admin > System > Manage Currency > Rates
  • Admin > System > Configuration > Currency Setup > Scheduled Import Settings > [Service]

That’s all there is to it. You can also set your cron to update currencies at automated intervals if you like.

Credit and thanks to Ryan Lynx: http://www.magentocommerce.com/wiki/5_-_modules_and_development/using_yahoo_finance_for_currency_exchange_rate_retrieval

WordPress › Error

There has been a critical error on your website.

Learn more about debugging in WordPress.