Contributions
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
I have made a small change to the script designed by "wagen".
1. Different url to receive the xml file
2. Changed
$GBP_EUR = round(1 /$exchrate[GBP], 8);
into
$GBP_EUR = round(1 * $exchrate[GBP], 8);
and
$GBP_USD = round(GBP_EUR *$exchrate[USD], 8);
into
$GBP_USD = round(1 * $exchrate[USD], 8);
3. Changed tep_db_query("update currencies set value = ". $GBP_EUR ." where code = 'EUR'");
into
tep_db_query("update currencies set value = ". $GBP_EUR ." where code = 'GBP'");
This is not a code file; it simply has some enhancements, based on v1.1 code.
It contains an example of defining the destination location for the xml file, as well as an example of keeping track of the exchange rate update.
You need to implement these on your own; should you find them useful that is.
The version has been updated to streamline and shorten the code. The main improvement is that the script does not read the rate file and update the database with every pageload, which is only necessary once every few hours.
There was a minor bug here:
if (time()- filemtime("eurofxref-daily.xml") > 36000) {
^ space missing in code
error in syntax replace quotes " by ' round $value1 in db queries e.g.
tep_db_query("update currencies set value = ". $value1 ." where code = 'EUR'");
tep_db_query("update currencies set value = '. $value1 . ' where code = 'EUR'");
The code has two parts, the 1st part is reading exchange rate and make a catch file in your web server. The 2nd part is updating your currency exchange rate.
Due to the exchange rate came from Euro bank, we need to do some calculations for other currency.
However, the code is written for GBP web site, therefore, little modification need to be made if you want to use USD or other currency as your default currency.
// 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];
}
}
// Get exchange rate for EUR based on your default currency.
$value1 = round(1 / $exchrate[GBP], 8); // Change $exchrate[***] to your default currency
// Get exchange rate for extra currency based on your default currency
$value2 = round($value1 * $exchrate[USD], 8); // Change $exchrate[***] to any other extra currency
tep_db_query("update currencies set value = ". $value1 ." where code = 'EUR'");
tep_db_query("update currencies set value = ". $value2 ." where code = 'USD'");
// Change ‘USD’ to any other currency you defined for value2
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'");
**********************************
Note: Contributions are used at own risk.