Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Currency Converter


iceblue

Recommended Posts

Hi folks!

 

The title says it all: I'm looking for a good contribution for updating the OSC currency exchange rates automatically so that my prices are always "the latest". I have tried some contributions for this issue by now but they simply don't want to work in that way I expect them to do. Does anybody has an idea for such a contribution?

 

Any help would be highly appreciated! ;)

 

Thank you very much in advance!

Link to comment
Share on other sites

Place this code in a file in your Admin directory and run it with a CRON schedule.

 

Original contrib is here: http://www.oscommerce.com/community/contri...date+currencies

 

<?php

/*

$Id: update_currencies.php, v0.5 2003/06/25 20:36:48 hpdl Exp $

 

osCommerce, Open Source E-Commerce Solutions

http://www.oscommerce.com

 

Copyright ? 2003 osCommerce

 

Released under the GNU General Public License

*/

 

// CONFIGURABLE VARIABLES

// Enable / Disable Reporting

// 0=none, 1=screen, 2=email, 3=both

$output_report = 2;

 

// Whatever you set for $output_report above will be overridden if you set output_report in the URL

// Defaults to 1

if (isset($_GET['output_report'])) {

switch ($_GET['output_report']) {

case '0':

case '2':

case '3':

$output_report = (int)$_GET['output_report'];

break;

default:

$output_report = 1;

break;

}

}

 

// Make sure this script has a way of downloading the exchange rate file

if (!function_exists(curl_init) && ((!function_exists(file_get_contents) && !function_exists(fopen)) || !ini_get('allow_url_fopen'))) {

die('The necessary functions and/or programs that this script needs are not installed on the server. Please tell your webhost that you need cURL installed or allow_url_fopen enabled in php.ini');

}

 

// Include application configuration parameters

require_once('includes/configure.php');

require_once(DIR_WS_INCLUDES . 'database_tables.php');

require_once(DIR_WS_FUNCTIONS . 'database.php');

 

// make a connection to the database... now

tep_db_connect() or die('Unable to connect to database server!');

// end of edit by mr_absinthe

 

 

// Only pull the config keys it needs

$config_query_raw = "select configuration_key as cfgKey, configuration_value as cfgValue from " . TABLE_CONFIGURATION . " where" .

" configuration_key = 'STORE_NAME' OR " .

" configuration_key = 'STORE_OWNER_EMAIL_ADDRESS' OR " .

" configuration_key = 'USE_DEFAULT_LANGUAGE_CURRENCY' OR " .

" configuration_key = 'LANGUAGE_CURRENCY' OR " .

" configuration_key = 'DEFAULT_CURRENCY'";

 

$configuration_query = tep_db_query($config_query_raw);

while ($config = tep_db_fetch_array($configuration_query)) {

define($config['cfgKey'], $config['cfgValue']);

}

 

$report = '';

 

if (DEFAULT_CURRENCY != '') {

// Pulls an XML file from currencysource.com based on your default currency

$er_url = "http://currencysource.com/RSS/" . DEFAULT_CURRENCY . ".xml";

$rates = '';

 

// Try using fopen since it's more supported

if (ini_get('allow_url_fopen')) {

// file_get_contents() only exists in PHP 4.3.0 and above

if (function_exists(file_get_contents)) {

$rates = file_get_contents($er_url);

} else {

$fh = fopen($er_url, "rb");

 

while (!feof($fh)) {

$rates .= fread($fh, 8192);

}

fclose($fh);

}

// Otherwise, use cURL

} else {

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $er_url);

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

$rates = curl_exec ($ch);

curl_close ($ch);

}

 

// Parse the XML file down to the important stuff and break it up into an array

$rates = substr($rates, strpos($rates, '<item>'), -20);

$rates = explode("\n", $rates);

 

// Simple check to make sure we didn't just download a 404 page

if (sizeof($rates) > 200) {

foreach ($rates as $rate) {

 

// Another simple check to make sure we're reading the correct data

if (strpos($rate, '<title>') !== false) {

// This could be done in one painfully complex regexp statement, but I enjoy my sanity

$rate = str_replace('<title>', '', $rate);

$rate = str_replace('</title>', '', $rate);

$rate = str_replace(' (', '|', $rate);

$rate = str_replace(')', '', $rate);

$rate = trim(substr($rate, 8));

 

$rate_vals = explode("|", $rate);

 

// This isn't really necessary, but just a little added security

$rate_vals[0] = trim(preg_replace('/[^A-Z]/i', '', $rate_vals[0]));

$rate_vals[1] = @floatval($rate_vals[1]);

 

// If the currency is numeric, update the database.

if (is_numeric($rate_vals[1])) {

// Because inputting the wrong information could really screw up a store, it needs an exact currency name match to work

$currency_query = tep_db_query("select currencies_id from " . TABLE_CURRENCIES . " where code = '" . $rate_vals[0] . "'");

 

//if (tep_db_num_rows($currency_query) > 0) { // original line

if ((tep_db_num_rows($currency_query) > 0) && ($output_report = 2)) { // changed to show output to screen regardless

 

$report .= 'Currency Value Updated: <b>' . $rate_vals[0] . ' to ' . $rate_vals[1] . '</b>   /  ' . "\n"; // for screen

 

tep_db_query("update " . TABLE_CURRENCIES . " set value = '" . $rate_vals[1] . "', last_updated = now() where code = '" . $rate_vals[0] . "' limit 1");

}

}

}

}

}

}

 

// If there's anything to report and the user has outputting enabled, display the info

if ($report && $output_report > 0) {

 

$messageStack->add($report .= '', 'success'); // added to show output to screen regardless of report choice

 

$report .= "\n" . 'Currencies Updated Status: SUCCESS' . "\n" . 'Currency Used for Base Conversions: ' . DEFAULT_CURRENCY . "\n" . 'Updated from URL: ' . $er_url;

 

$report = STORE_NAME . ' Currencies Updated on ' . date("D M j G:i:s Y") . "\n" . $report;

 

if ($output_report != 2) {

echo str_replace("\n", '<br>', $report);

}

 

if ($output_report != 1) {

tep_mail(STORE_EMAIL_HEADER, STORE_OWNER_EMAIL_ADDRESS, 'Currency Rates updated for ' . STORE_NAME, stripslashes($report), STORE_EMAIL_HEADER, STORE_OWNER_EMAIL_ADDRESS);

}

}

?>

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...