For those interested in a more expanded set of currencies, here is my version. Note that you have to sign up (free) for an API key at openexchangerates.org and enter that in.
Code:
<?php
/*
* Automatic Currency Update v6.0
* Originally by Richard Fink (masterblaster) based on Zen Cart manual currency update
* updated by Kuroi to include Zen Cart's currency uplift ratio
* further updated by Kuroi to use European Central Bank reference rates (adapted from ECB-supplied code)
* updated by Zen4All (design75) use Zen Cart standard coding
*
* Rework for PHP 5.3 compatibility (ereg and mysql_db_query functions deprecated in PHP 5.3.0)
* switched to using SimpleXML and mysqli instead.
* JeRo www.jero.co.nz 18/06/2010
*
* modified by s_mack to use openexchangerates instead of very limited ECB. Simplified some logic.
* license: GNU Public License V2.0
*
*/
require_once('includes/application_top.php');
// Sign up for openechangerates.org API and enter your ID below
$openexhangeratesapi = "";
/*
* Get default currency.
*/
$defaultCurrency = $db->Execute("SELECT configuration_value
FROM " . TABLE_CONFIGURATION . "
WHERE configuration_key = 'DEFAULT_CURRENCY' LIMIT 1 ");
$defaultCurrency = $defaultCurrency->fields['configuration_value'];
/*
* Get currency conversion ratio.
*/
$currencyUpliftRatio = $db->Execute("SELECT configuration_value
FROM " . TABLE_CONFIGURATION . "
WHERE configuration_key = 'CURRENCY_UPLIFT_RATIO' LIMIT 1 ");
//grab the full dataset from openechangerates.org
$currency_data = file_get_contents('http://openexchangerates.org/api/latest.json?app_id=' . $openexhangeratesapi);
$currency_data = json_decode($currency_data, true);
//since USD is not represented in the dataset, lets push it in
$currency_data['USD'] = 1;
// extract your default currency's exchange rate.
$defaultRate = (float)$currency_data['rates'][$defaultCurrency];
// put each currency into the DB, adjusted with the currency uplift ratio
foreach ($currency_data['rates'] as $code => $rate) {
$rate = ( $code == $defaultCurrency ? 1 : (float)$rate * $currencyUpliftRatio->fields['configuration_value'] / $defaultRate );
$sql_data_array = array('value' => $rate,
'last_updated' => 'now()',
'code' => $code);
zen_db_perform(TABLE_CURRENCIES, $sql_data_array, 'update', "code = '" . $code . "'");
}
You'll notice in the comments I took the liberty of calling this V6 and adding my name... I did rewrite a bit of the logic to clean it up, in my opinion, as it was sort of making redundant-ish calls. Not important. I'm not uploading it to the downloads section so use or don't.
Bookmarks