Contributions

Features (Category Index)
Search: 

Auto Update Currency Exchange Rate

The following script is catching the latest exchange rate from European Central Bank. The script will generate an XML file in order to process everything as quick as possible.

All you need to do is simple copy the following code into APPLICATION_TOP.PHP in INCLUDES folder. And then, everytime the customer visit your shop, the currency exchange rate will be automatically updated to latest value.

CODE:

**********************************

// Update Currency Rate

# Read currency exchanges rates
# Cache file if appropriate ...
if (time()-filemtime("eurofxref-daily.xml") > 36000) {
$stuff = file("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
$fh = fopen("eurofxref-daily.xml","w");
foreach ($stuff as $line) { fputs($fh,$line); }
fclose ($fh);
$xld = "loaded afresh (and not cached)";
} else {
# .. or read from cache
$stuff = file("eurofxref-daily.xml");
$xld = "cached (and not loaded afresh)";
}
# $xld may be used in your output to inform you user or admin
# Extract exchange rates
$exchrate[EUR] = 1.00;
foreach ($stuff as $line) {
ereg("currency='([[:alpha:]]+)'",$line,$gota);
if (ereg("rate='([[:graph:]]+)'",$line,$gotb)) {
$exchrate[$gota[1]] = $gotb[1];
}
}

$GBP_EUR = round(1 / $exchrate[GBP], 8);
$GBP_USD = round($GBP_EUR * $exchrate[USD], 8);

tep_db_query("update currencies set value = ". $GBP_EUR ." where code = 'EUR'");
tep_db_query("update currencies set value = ". $GBP_USD ." where code = 'USD'");

**********************************

Expand All / Collapse All

Minor Bug 29 Aug 2009

There was a minor bug here:

if (time()- filemtime("eurofxref-daily.xml") > 36000) {
^ space missing in code

Auto Update Currency Exchange Rate bugfix 15 Feb 2008
Explaination of the code 13 Feb 2008
Auto Update Currency Exchange Rate 25 Jan 2008

Note: Contributions are used at own risk.