Community Add-Ons

Add file to this package
Top » Features

Auto Update Currency Exchange Rate
for osCommerce Online Merchant v2.2

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.



// Update Currency Rate

# Read currency exchanges rates
# Cache file if appropriate ...
if (time()-filemtime("eurofxref-daily.xml") > 36000) {
$stuff = file("");
$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) {
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'");


Legend:  Download   Report

Expand All / Collapse All

Update to Integrate with Native OSC Currency Support and work with all Store Currencies (not only EUR or GBP) 17 Oct 2012  

Hi all

I've modified this contribution to do the following:

- Integrate with the native Currency functionality of OSC (ie when you go to Admin->Localization->Currencies) it will update the currencies using the European Central bank rates
- Because it integrates with the native Currencies functionality, it will work with as many Currencies as you want. Just use the OSC's Localization->Currency functionality to add and manage currencies.

- Will work with all stores, regardless of what the Default Currency is (ie the store default currency doesn't have to be EUR or GBP). It will auto detected the store's native currency and calculate the exchange rates correctly.

- Will work with all currencies provided by EC Bank (EUR, GBP, USD, CAD, HKD, and many more ....; it won't work with currencies not listed by the EC Bank).


Step 1) Edit /admin/includes/functions/localization.php

At the end of the document, paste the following code (before the "?>" line):

function quote_ECBank_currency($to) {
# Read currency exchanges rates
$xmlcontents = file("");

# $xld may be used in your output to inform you user or admin
# Extract exchange rates
foreach ($xmlcontents as $line) {
if (ereg("rate='([[:graph:]]+)'",$line,$gotb)) {
$exchrate[$gota[1]] = $gotb[1];
$exchrate['EUR'] = 1; /* manually add 1 EUR = 1 EUR to the array (all Exch.Rates are from EUR to X because we're getting them from ECB*/

if (!array_key_exists(DEFAULT_CURRENCY, $exchrate)) {
return false; /* the Store Default currency must be present in the list because these exchange rates are based in 1EUR=xx
Therefore we need to calculate the rate from EUR to the Default currency and then from the Default currency to the destination currency */

if (!array_key_exists($to, $exchrate)) {
return false;

$DefaultCurr_to_EUR_Rate = round(1 / $exchrate[DEFAULT_CURRENCY], 8);
$DefaultCurr_to_DestCurr = round($DefaultCurr_to_EUR_Rate * $exchrate[$to], 8);

if (is_numeric($DefaultCurr_to_DestCurr) && $DefaultCurr_to_DestCurr > 0) { /* make sure we got a valid number */
return $DefaultCurr_to_DestCurr;
} else {
return false;

Save the file.

Step 2: Edit admin/includes/application_top.php

Find the line that starts with "define('CURRENCY_SERVER_PRIMARY', " and replace the whole line with:

Save the file.

All done.


Now go to the Admin section, select Localization and then Currencies.
Click Update.

You should see a message saying that all currencies were successfully updated via ECBank.

If you see an error about updating via ECBank, see the troubleshooting bellow:


1) If you see an error when testing it may be caused by 2 things:

A) Your server is not being able to fetch the file from the EC bank.
B) There's a poorly written sanity check in the file admin/currencies.php that may cause these issues.

To fix case B) do this:
Edit admin/currencies.php

Find the line that says "if (tep_not_null($rate)) {" and replace the whole line with:
if (is_numeric($rate) && $rate > 0) {

Save the file and test again.

2) You may also want to adjust your Exchange Rate BACKUP server (in case the updating via the primary server - ECBank - fails).
In admin/includes/application_top.php

Find the line that looks like "define('CURRENCY_SERVER_BACKUP', 'XXXX');"
replace 'XXXX' with 'oanda' or 'xe' depending on which server you want to use as backup server.

I hope you find the contribution useful. :)

As always, BACKUP ALL FILES before modifying them.
Contributions used at your own risk. ( I will not be responsible for any direct or indirect damage or loss derived of using this contribution. )


Auto Update Currency Exchange Rate upd.1 5 Jan 2010  
Small enhancements 29 Dec 2009  
Auto Update Currency Exchange Rate v1.1 (Full Package) 21 Dec 2009  
Minor Bug 29 Aug 2009  
Auto Update Currency Exchange Rate bugfix 15 Feb 2008  
Explaination of the code 13 Feb 2008  
Auto Update Currency Exchange Rate 25 Jan 2008