Jump to content
Sign in to follow this  
Datse

Auto Update Currency Exchange Rate

Recommended Posts

Searching the forums I couldn't find any post dealing with this contribution: http://addons.oscommerce.com/info/5685%20" target="_blank"> http://addons.oscommerce.com/info/5685

This contrib seems ideal to get rid of complicated scripts to auto update currencies. It seems to work well but, I think has Pound as a base rate.

I need to use Euro as the standard currency, but would like to be able to show customers from England or the US prices in Dollar or Pound.

 

Any hint or even step-by-step explanation how to define what's getting updated would be highly appreciated

 

 

Datse

 

Here is the 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'");

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

Share this post


Link to post
Share on other sites

The following code is supposed to solve the problem of Euro as base currency (if so all credits go to wagen):

 

If you use EUR as your default currency,

please change the code as

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

$exchrate[EUR] = 1.00;
foreach ($stuff as $line) {
ereg("currency='([[:alpha:]]+)'",$line,$gota);
if (ereg("rate='([[:graph:]]+)'",$line,$gotb)) {
$exchrate[$gota[1]] = $gotb[1];
}
}

$EUR_GBP= round(1 / $exchrate[GBP], 8);
$EUR_USD = round(1 / $exchrate[USD], 8);

tep_db_query("update currencies set value = ". $EUR_GBP ." where code = GBP");
tep_db_query("update currencies set value = ". $EUR_USD ." where code = 'USD'");

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

Share this post


Link to post
Share on other sites

The above code didn't do the job, but after playing around I ended up with following coe that seems to do exactly what I wanted: starting with Euro and giving prices in USD and GBP

 

It would be great if one could change the base currency in admin -- but that is beyond my understanding ... :blink:

$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($exchrate[GBP] / 1, 8);
$USD_EUR = round($exchrate[USD] / 1, 8);

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

Share this post


Link to post
Share on other sites

Did you set the EUR as the base (default) currency in admin?

Edited by Coopco

The Coopco Underwear Shop

 

If you live to be 100 years of age, that means you have lived for 36,525 days. Don't waste another, there aren't many left.

Share this post


Link to post
Share on other sites
Did you set the EUR as the base (default) currency in admin?

 

 

I guess I didn't made it clear: The contrib can not be adjusted from within the admin - it would be great if that would be the case.

 

For my part the currency seems to be working after the above changes.

Share this post


Link to post
Share on other sites

I followed the manual instructions to add this (although to OSC2.3) and it seemed to be working fine but i am noticing on the site that occasionally, it gets an error in the line where it calls

 

$stuff = file("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");

and this results in five or six errors at the top of the homepage.

 

I'm guessing this is something to do with the website where the rates are either timing out or just not responding to the request.

 

I made one slight change which is to check the file modification time and only refresh if it is more than a day old (which i think was suggested on the contributions page)

 

Any suggestions on how to make it more resilient.

 

The code i used in application top is as follows and as i mentioned, most of the time it is working fine but at random, it gives a result like the screenshot. I've disabled it at the moment as i dont want users to get this error.

 

# Read currency exchanges rates and update once per day

$exchange_rate_file="eurofxref-daily.xml";

if (!file_exists($exchange_rate_file) || time()-filemtime($exchange_rate_file) > 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)";

# $xld may be used in your output to inform you user or admin

# Extract exchange rates

$exchrate[EUR] = 1.00;

foreach ($stuff as $line) {

preg_match('#currency=\'([[:alpha:]]+)\'#',$line,$gota);

if (preg_match('#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 ."*(1+(commission/100)) where code = 'EUR'");

tep_db_query("update currencies set value = ". $GBP_USD ."*(1+(commission/100)) where code = 'USD'");

}

 

I've attached a screenshot of the issue here.

exchange_rates.jpg

Edited by twjnorth

Share this post


Link to post
Share on other sites

Hey everybody I have been looking for a solution to a problem that I think is not far from what you're talking about..

I'm a beginner so please bare with me

so my default currency was the USD and I wanted to change it to Euro when I did that whenever I typed a price for a product for example 10 euro it would show up as 11.32 or something like that on the listing which means that whenever i type in a price it is converted..

I'm confused and really have no clue what to do or where to begin looking for a solution ..

any help is more than welcome.

Share this post


Link to post
Share on other sites

Have a look in the basic in stallation threads, how to change default currency in the first 25 posts.

 

HTH

 

G


Need help installing add ons/contributions, cleaning a hacked site or a bespoke development, check my profile

 

Virus Threat Scanner

My Contributions

Basic install answers.

Click here for Contributions / Add Ons.

UK your site.

Site Move.

Basic design info.

 

For links mentioned in old answers that are no longer here follow this link Useful Threads.

 

If this post was useful, click the Like This button over there ======>>>>>.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×