Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Automatic currency update


Malawi

Recommended Posts

Posted

Hi

 

Is there a way to update the currencies for instance every day or hour?

 

 

May I add a percentage or surcharge for some currencies?

 

If the exchange rate says that the price is EUR 10, I would like the price to be EUR 11 to reflect the extra cost and risk.

Malawi, The Fisher King

AKA Trygve Lillefosse

  • 3 weeks later...
Posted

My php server is not Compiled CGI and so I needed to create a php page that takes care of updating the currencies for me without being in the admin mode.

 

Here is my created page which must to be created under the catalog folder and can be access either from the browser or used in a CRON job.

 

You can call the page whatever you like (I called it 'update_currencies.php').

 

One thing, I don't know if there is any security issues in my approach. Would appreciate any feedback on this from those who know.

 

<?php
/*
?$Id: update_currencies.php,v 1.00 2004/12/10 09:10:05 hpdl Exp $

?osCommerce, Open Source E-Commerce Solutions
?http://www.oscommerce.com

?Copyright (c) 2004 [email protected]

?Released under the GNU General Public License
*/

?require('includes/application_top.php');
?define('CURRENCY_SERVER_PRIMARY', 'oanda');
?define('CURRENCY_SERVER_BACKUP', 'xe');

?function quote_oanda_currency($code, $base = DEFAULT_CURRENCY) {
? ?$page = file('http://www.oanda.com/convert/fxdaily?value=1&redirected=1&exch=' . $code . ?'&format=CSV&dest=Get+Table&sel_list=' . $base);

? ?$match = array();

? ?preg_match('/(.+),(\w{3}),([0-9.]+),([0-9.]+)/i', implode('', $page), $match);

? ?if (sizeof($match) > 0) {
? ? ?return $match[3];
? ?} else {
? ? ?return false;
? ?}
?}

?function quote_xe_currency($to, $from = DEFAULT_CURRENCY) {
? ?$page = file('http://www.xe.net/ucc/convert.cgi?Amount=1&From=' . $from . '&To=' . $to);

? ?$match = array();

? ?preg_match('/[0-9.]+\s*' . $from . '\s*=\s*([0-9.]+)\s*' . $to . '/', implode('', $page), $match);

? ?if (sizeof($match) > 0) {
? ? ?return $match[1];
? ?} else {
? ? ?return false;
? ?}
?}

// ? require('includes/classes/currencies.php');

$currencies = new currencies();

?	$server_used = CURRENCY_SERVER_PRIMARY;

$currency_query = tep_db_query("select currencies_id, code, title from " . TABLE_CURRENCIES);

while ($currency = tep_db_fetch_array($currency_query)) {
? $quote_function = 'quote_' . CURRENCY_SERVER_PRIMARY . '_currency';
? $rate = $quote_function($currency['code']);

? if (empty($rate) && (tep_not_null(CURRENCY_SERVER_BACKUP))) {
?$messageStack->add_session(sprintf(WARNING_PRIMARY_SERVER_FAILED, CURRENCY_SERVER_PRIMARY, $currency['title'], $currency['code']), 'warning');

?$quote_function = 'quote_' . CURRENCY_SERVER_BACKUP . '_currency';
?$rate = $quote_function($currency['code']);

?$server_used = CURRENCY_SERVER_BACKUP;
? }

? if (tep_not_null($rate)) {
?tep_db_query("update " . TABLE_CURRENCIES . " set value = '" . $rate . "', last_updated = now() where currencies_id = '" . (int)$currency['currencies_id'] . "'");

?$messageStack->add_session(sprintf(TEXT_INFO_CURRENCY_UPDATED, $currency['title'], $currency['code'], $server_used), 'success');
? } else {
?$messageStack->add_session(sprintf(ERROR_CURRENCY_INVALID, $currency['title'], $currency['code'], $server_used), 'error');
? }
}
//tep_redirect(tep_href_link(FILENAME_DEFAULT, '', 'SSL'));
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
</head>
<body>
<?php
?if ($messageStack->size > 0) {
? ?echo $messageStack->output();
?}
?>
</body>
</html>

Posted

Another way to skin this cat is to add a 0x0 image on an admin page, link it (the image) to the .php page you want to call to run the maintenance, and set the admin page to refresh every hour (or whatever). Yet another way is to have a program like URLy Warning or Karen's Net Monitor call the page (it can be the page with the 0x0 image or the actual .php page you want to call to run the maintenance) every hour or whatever. There are also some third-party sites that will run this type of cron job for free.

 

I'll leave it to the experts to comment on the relative advantages or disadvantages of these various approaches.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...