Datse Posted February 12, 2008 Share Posted February 12, 2008 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'"); ********************************** Quote Link to comment Share on other sites More sharing options...
Jack_mcs Posted February 12, 2008 Share Posted February 12, 2008 Try this one instead. It works fine and is easy to use. Jack Quote Support Links: For Hire: Contact me for anything you need help with for your shop: upgrading, hosting, repairs, code written, etc. Get the latest versions of my addons Recommended SEO Addons Link to comment Share on other sites More sharing options...
Datse Posted February 13, 2008 Author Share Posted February 13, 2008 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'"); ********************************** Quote Link to comment Share on other sites More sharing options...
Datse Posted February 13, 2008 Author Share Posted February 13, 2008 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'"); Quote Link to comment Share on other sites More sharing options...
Guest Posted February 13, 2008 Share Posted February 13, 2008 (edited) Did you set the EUR as the base (default) currency in admin? Edited February 13, 2008 by Coopco Quote Link to comment Share on other sites More sharing options...
Datse Posted February 13, 2008 Author Share Posted February 13, 2008 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. Quote Link to comment Share on other sites More sharing options...
twjnorth Posted November 14, 2011 Share Posted November 14, 2011 (edited) 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. Edited November 14, 2011 by twjnorth Quote Link to comment Share on other sites More sharing options...
hamzter Posted November 22, 2011 Share Posted November 22, 2011 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. Quote Link to comment Share on other sites More sharing options...
♥geoffreywalton Posted November 23, 2011 Share Posted November 23, 2011 Have a look in the basic in stallation threads, how to change default currency in the first 25 posts. HTH G Quote 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 ======>>>>>. Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.