In a bid to find a more reliable source for currency exchange rates and faster servers to boot, I’ve created this Module to get rates from Yahoo! Finance. Note that this may be subject to Yahoo! T’s & C’s so you better read them before using this.
I’ve made this upgrade-proof to the best of my knowledge; it uses local xml and Module code. But of course, do take back-ups and test your back-ups. This is supplied as is and is not implied to work at all. You know the drill.
Please Note This is not available on Magento Connect. Installation consists of just one new file and one local file edit. Command line with vim or any FTP enabled text editor will do the trick nicely.
Step 1 – Create Folders & File Structure
I have mimicked the Webservicex currency retrieval code. That one resides in /app/code/core/Mage/Directory/Model/Currency/Import/Webservicex.php so you can have a look yourself. In line with that code and structure, first create the following folder structure and file:
[code]/app/code/local/JT/Directory/Model/Currency/Import/Yahoofinance.php[/code]
Step 2 – Paste Code into Yahoofinance.php
[code]<?php
/**
* JT
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* @category JT
* @package JT_Directory
* @copyright Copyright (c) 2009 JT
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*
* All credits to Magento Core Development Team who’s Webservicex code I liberally borrowed
* as well as Mark Alexander Bain’s idea and code as displayed on the following url, accessed Feb 16th 2009
* http://ajax-programming.suite101.com/article.cfm/currency_conversion_with_yahoo_finance
*/
/**
* Currency rate import model (From quote.yahoo.com)
*
* @category JT
* @package JT_Directory
* @author Magento Enthusiast <J.T.>
*/
class JT_Directory_Model_Currency_Import_Yahoofinance 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); //Be nice to Yahoo, they don’t have a lot of hi-spec servers
$handle = fopen($url, "r");
$exchange_rate = fread($handle, 2000);
fclose($handle);
if( !$exchange_rate ) {
$this->_messages[] = Mage::<span>helper(‘directory’)->__(‘Cannot retrieve rate from %s’, $url);
return null;
}
return (float) $exchange_rate * 1.0; // change 1.0 to influence rate;
}
catch (Exception $e) {
if( $retry == 0 ) {
$this->_convert($currencyFrom, $currencyTo, 1);
} else {
$this->_messages[] = Mage::<span>helper(‘directory’)->__(‘Cannot retrieve rate from %s’, $url);
}
}
}
} [/code]
Step 3 – Amend local.xml
That’s /app/etc/local.xml – add this inside the <global> tag (at the end of it, before the </global> tag):
[code]
<!– JT Yahoo Finance Integration–>
<currency>
<import>
<services>
<yahoofinance>
<name>Yahoo Finance</name>
<model>JT_Directory_Model_Currency_Import_Yahoofinance</model>
</yahoofinance>
</services>
</import>
</currency>
<!– EOF JT Yahoo Finance Integration–>
[/code]
Step 4 – Run it!
(You may have to flush your Configuration Cache first: System > Cache Management – Untick Configuration > Save > Tick it again > Save)
In your Magento Admin go to System > Manage Currency Rates
In the drop-down, Webservicex is still the top one so simply select Yahoo Finance from there and click the Import button.
Step 5 – Optional – Cron Setup
In Admin under System > Configuration go to Currency Setup under the Default Configuration scope. Enable the cron for Yahoo Finance here under Scheduled Import Settings (I’ve not yet tested this).
Important Notes
Just to not piss off Yahoo, there’s a sleep(1) one second interval built in. If you only use two or three currencies, you won’t even notice this delay. If like me you use 23 currencies, this may make it look slow but at least you’ll avoid potential auto-throttling and/or Yahoo! firewall banning by making requests look more “real”.
Yahoo! supplies these rates in csv files to the public. It’s up to you whether you ass-u-me that you can now use them in an automated fashion for commercial benefit or kindly request permission, or at least read the small print.
Note line 55 where the exchange rate is being returned. By default it’s returned as is, but some may want to influence this. For example, right now Yahoo! reckons one GBP is worth 1.1157 EUR. A 100 Pound item will be displayed as 115.70 Euro. When paying by bank transfer or credit card, they will be surprised when they read their bank statements as the bank’s rate will be more like 1.19 so they get deducted the grand sum of 119 Euro for that product. They may think you are falsely advertising the rates and pocketing the difference, even though your T’s & C’s will mention this possible discrepancy. So with this rate influencer ratio, you can dampen the fun a bit by using for example 1.049 instead of 1.0 (that allows for 4.9% mark-up by the banks). Now the products will look more expensive but there won’t be any surprises on their bank statements.
Source :http://www.magentocommerce.com/
Hi Blogger,When you note some article and sharingwith me,that is a hard work for you but share makes you happly right?good luck and cheers!
Thank you …
Hi,thanks for the tutorial.I want to ask,is it possible to import currency rates from local bank in my country where they do not have currency converter like Yahoo! did..Thanks..
I really do have to admit it’s quite refreshing to uncover a cool blog such as this one, excellent effort. I I hope to be coming back again soon and I’ll be watching out for your next comment when I do, bye for now.
F*ckin’ remarkable things here. I’m very glad to see your article. Thanks a lot and i’m looking forward to contact you. Will you kindly drop me a mail?
Magnificent points altogether, you simply received a new reader. What could you suggest in regards to your publish that you made some days ago? Any sure?
A lot of of whatever you assert happens to be supprisingly appropriate and that makes me wonder why I hadn’t looked at this with this light before. This particular article truly did turn the light on for me as far as this particular issue goes. Nonetheless there is actually 1 point I am not really too cozy with and while I try to reconcile that with the actual main idea of your issue, let me see exactly what the rest of your visitors have to say.Very well done.
Hey! I just want to give a huge thumbs up for the good information you’ve gotten here on this post.
I will likely be coming back to your blog for
extra soon.
Whats up! I just wish to give a huge thumbs up for
the good data you’ve gotten right here on this post.
I shall be coming back to your weblog for more soon.
Hey! I just want to give an enormous thumbs up for the good data you might have right here on
this post. I can be coming back to your weblog for extra soon.
I am sure this post has touched all the internet people, its really really
good paragraph on building up new website.