I decided to do this because someone was offering this on the forum at a cost, I needed this function for myself anyway, it took me about half an hour to code.
If you find this useful please donate to paypal@hm2k.org
I called the file "update_currency.php".
Enjoy...
Code: Select all
<?php
//OpenCart Currency Updater v0.1 by HM2K
//If you find this useful please donate to paypal@hm2k.org
// Configuration
require('config.php');
// Locator
include(DIR_LIBRARY . 'locator.php');
$locator =& new Locator();
// Config
$config =& $locator->get('config');
// Database
$database =& $locator->get('database');
// Settings
$settings = $database->getRows("select * from setting where type = 'catalog' or type = 'global'");
foreach ($settings as $setting) {
$config->set($setting['key'], $setting['value']);
}
// Request
$request =& $locator->get('request');
//Rest Of Code
$default=$config->get('config_currency');
echo "Default code is $default<br>\n";
$sql = "select currency_id, title, code, value, date_modified from currency";
$sql .= " order by title asc";
$results = $database->getRows($sql);
foreach ($results as $result) {
$id=$result['currency_id'];
$code=$result['code'];
$value=$result['value'];
$new=ucc(1,$default,$code);
echo "$code = $value => $new <br>\n";
$sql = "update currency set value = '?', date_modified = now() where currency_id = '?'";
$database->query($database->parse($sql, $new, $id));
}
function ucc ($amount,$from,$to) {
$url='http://www.xe.com/ucc/convert.cgi?Amount=%s&From=%s&To=%s';
$url=sprintf($url,$amount,$from,$to);
ini_set('user_agent','Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.7.12) Gecko/20050919 Firefox/1.0');
$c=file_get_contents($url);
$c=strip_tags($c);
preg_match("/1 $from = (.+?) $to/s",$c,$matches);
return $matches[1];
}
?>