Jump to content
Jack_mcs

Auto Update Currencies

Recommended Posts

Hi there Jack

 

@@Psytanium

 

Here's the code I use on a 2.3.4 site - has the mysqli stuff in there

<?php
/*
  Auto Update Currencies v 1.0 by Jack_mcs - oscommerce-solution.com
  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/
  ///////CONFIGURABLE VARIABLES///////////
  define('DEFAULT_CURRENCY', 'GBP');
  define('HOURS_BETWEEN_UPDATES' , '24'); //if more than one day, multiply days by hours so the setting for two days would be 2 * 24 = 48
  define('REPORT_NO_UPDATE', true); //if the currencies are not updated since they have already been updated, don't report it. Set to false to always receive this message
  
  define('NO_REPORT',    0);
  define('SCREEN',       1);
  define('EMAIL',        2);
  define('SCREEN_EMAIL', 3);
  
  $output_report = EMAIL;
  ///////////////END//////////////////////

  define('CURRENCY_SERVER_PRIMARY', 'xe');
  define('CURRENCY_SERVER_BACKUP', 'oanda');

  require_once('includes/configure.php');
  require_once(DIR_WS_FUNCTIONS . 'localization.php');




  $link = mysqli_connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD,DB_DATABASE) or die("Unable to connect to database server!");
  //mysql_select_db(DB_DATABASE);

  $report = '';
  $updated = array();
  $rate = '';
  $today = date("Y-m-d H:i:s"); // The exact time
  $currency_query = mysqli_query($link,"select currencies_id, code, title, last_updated from currencies" ) or die(mysql_error());

  while ($currency = mysqli_fetch_array($currency_query, MYSQL_ASSOC))
  {
    
    {
      $quote_function = 'quote_' . CURRENCY_SERVER_PRIMARY . '_currency';
      $rate = $quote_function($currency['code']);

      if (empty($rate)) {
        $quote_function = 'quote_' . CURRENCY_SERVER_BACKUP . '_currency';
        $rate = $quote_function($currency['code']);
      }

      if (!empty($rate)) {
        $updated[] = 'The exchange rate for ' . $currency['title'] . ' <b>WAS</b> successfully updated.' . "\n";
		//if ($rate !=1){$rate=$rate*1.02;}
        mysqli_query($link,"update currencies set value = '" . $rate . "', last_updated = now() where currencies_id = '" . (int)$currency['currencies_id'] . "'");
      }
      else
        $updated[] = 'The exchange rate for ' . $currency['title'] . ' <b>WAS NOT</b> successfully updated. It was last updated on ' . $currency['last_updated'] . "\n";
    }

  }

  if ($output_report > NO_REPORT) {
    $report = 'Currencies Update Report' . "\n\n";

    $configuration_query = mysqli_query($link,"select configuration_value as store_name from configuration where configuration_key = 'STORE_NAME' limit 1") or die(mysqli_error());
    $configuration = mysqli_fetch_array($configuration_query, MYSQL_ASSOC);
    $report .= 'Currencies for ' . $configuration['store_name'] . ' updated on ' . date("D M j G:i:s Y") . "\n\n";
 
    foreach ($updated as $changed)
      $report .= $changed;

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

    
  }
  mysqli_close($link);
?>

n.b I have an extra quick and dirty line in there (if ($rate !=1){$rate=$rate*1.02;}) that adds 2% markup on the exchange rate as my payment processor adds that in as their premium over and above the bank rates.

 

edit: commented out that line but left it in there FYI

 

Dirty trick there, so to make it even dirtier how to specify specific countries or currencies?


Getting the Phoenix off the ground

Share this post


Link to post
Share on other sites

If you just want to update one particular currency, change this line

 $currency_query = mysqli_query($link,"select currencies_id, code, title, last_updated from currencies" ) or die(mysql_error());

to this

 $currency_query = mysqli_query($link,"select currencies_id, code, title, last_updated from currencies where currencies_id=XX " ) or die(mysql_error());

where xx is the ID of the currency you want to check.

Share this post


Link to post
Share on other sites

Hi,

 

I'm now getting the following error, any ideas please?

Warning: file_get_contents(http://www.xe.net/ucc/convert.cgi?Amount=1&From=GBP&To=EUR) [ function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /hosting/www/site.com/www/admin/includes/functions/localization.php on line 25

Warning: file_get_contents(http://www.oanda.com/convert/fxdaily?value=1&redirected=1&exch=EUR&format=CSV&dest=Get+Table&sel_list=GBP) [ function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /hosting/www/site.com/www/admin/includes/functions/localization.php on line 14

 

Currencies are not being updated.


Absinthe Original Liquor Store

Share this post


Link to post
Share on other sites

Thank you again Jack for pointing me the right direction, after applying Gary's fix, there are no errors when updating currencies via admin, however still the same errors when running a cron and auto_update_currencies.php file. I guess that the file has to be also changed according to Gary's fix... Is it enough to change this?

from:

define('CURRENCY_SERVER_PRIMARY', 'oanda');

to:

define('CURRENCY_SERVER_PRIMARY', 'fixer');

most likely not, because currency update report is received by email with text Currencies for store updated on Tue Dec 13 8:56:56 2016, however when looking in admin, they are not updated (date doesn't match).

Edited by mr_absinthe

Absinthe Original Liquor Store

Share this post


Link to post
Share on other sites

A new version has been uploaded with these changes:

 

  • Added code to account for changes in hours being over one day.
  • Added an option and code to allow the update to be ran from admin instead of cron.
  • Changed code to work with the fixer server.
  • Changed code so date function returns hours instead of days.
  • Changed php shortcut <?

Share this post


Link to post
Share on other sites

Hoping someone can help.

 

I have been out of oscommerce and php too many years and really have lagged behind the times.

 

 

I have a friend running a 2.3.4 that was running this contrib, but only realised today errors in updating currencies as it was not updatred by the folks looking after it now!

 

PHP Version: 5.3.29 (Zend: 2.3.0)  Database: MySQL 5.5.54-cll

 

I have done the fixes on github to the relevant files - the manual update button on the currencies page works fine via the fixer server and have uploaded the latest version of this contrib so he can control it via his admin panel and I also tried it via crontab. 

 

However, it is throwing up an error:

 

"Fatal error: Call to undefined function tep_db_fetch_array() in /home/xxxxxxxx/public_html/backend/auto_update_currencies.php on line 36"

 

and for the life of me I cannot remember how to fix it.

 

Any help very much appreciated as always, as I am unable to spend much time on this as I have just undergone hand surgery and it is killing me!

Edited by RobC1962

Share this post


Link to post
Share on other sites

That error refers to a standard function and it is saying the function is missing. The latest version of this addon checks for a server variable to determine if the code is ran by cron or not. It might be that that variable isn't set on your server, though it should be. Try editing the file and change this code

      require('includes/application_top.php');
  }

to this

  }
      require('includes/application_top.php');

Share this post


Link to post
Share on other sites

I have just uploaded your latest auto_update_currencies addon, cannot run it because it was initially complaining about a missing date_diff function. I then source the function from ver 1.1 but updated the last line with the recommended change to " return $nhours". Now I'm getting the following error messages:

 

Warning: strtotime() expects parameter 1 to be string, object given in ~/admin/auto_update_currencies.php on line 29

Warning: strtotime() expects parameter 1 to be string, object given in ~/admin/auto_update_currencies.php on line 30

Fatal error: Call to a member function format() on a non-object in ~/admin/auto_update_currencies.php on line 98

 

Any idea what I may be doing wrong? ... 

Share this post


Link to post
Share on other sites

Hi @@Jack_mcs, I replaced the old file with the new release Jan 2017, i got the Fatal error: Call to undefined function tep_db_query() error.

 

I did what you proposed, I move require('includes/application_top.php'); out of if statement.

 

I run the script directly from https://www.domain.com/admin/auto_update_currencies.php

 

Now the error is gone, but should i leave it like this ?

 

Thanks

Share this post


Link to post
Share on other sites

It won't hurt to leave it like that as long as it runs. The script should be able to run from cron as well as manually. It both work, or if you only need one to work, then it's fine.

Share this post


Link to post
Share on other sites

I have installed V1.2 and also gone into /admin/includes/filenames.php to update 'FILENAME_CURRENCIES' to 'auto_update_currencies.php'. 

 

When I try to open the currencies update from admin I get this error:

 

Fatal error: Call to undefined function date_diff() in /home/content/78/4534778/html/admin/auto_update_currencies.php on line 83

Share this post


Link to post
Share on other sites

FILENAME_CURRENCIES and 'auto_update_currencies.php' are totally different items. If you want to change FILENAME_CURRENCIES it would be 'currencies.php' (apostrophes required).

Share this post


Link to post
Share on other sites

Hi Jack!

 

This is the email message I get from trying to run the cron job:

 

Fatal error</b>: Call to undefined function date_diff() in <b>/~/admin/auto_update_currencies.php</b> on line <b>83</b>

Share this post


Link to post
Share on other sites

This version of the addon will only work for php versions of 5.3 or greater. If your server is using less than 5.3, then you will need to use an earlier version of the addon. Also, what about if you run it manually? Does it fail that way too?

 

If it fails by running it manually and your servers is using 5.3 or greater, please try this. In the admin/auto_update_currencies.php file, find

$interval = date_diff($date_begin,$date_end);

and change it to

 $interval = $date_begin->diff($date_end);

I haven't tested the above but I think it will work.

Share this post


Link to post
Share on other sites

Thanks again Jack ... and yes the php version I have is 5.2.17 and therefore I'll try and use v1.1 addon. And yes even if you try and run it manually or with the change you have suggested above, the problem will still occur.

 

I will let you know how I go with v1.1 addon.

Share this post


Link to post
Share on other sites

Hello Jack,

what could be the reason ?

<br />
<b>Warning</b>:  session_start(): Cannot send session cookie - headers already sent in <b>/home/user/public_html/admin/includes/functions/sessions.php</b> on line <b>98</b><br />
<br />
<b>Warning</b>:  session_start(): Cannot send session cache limiter - headers already sent in <b>/home/user/public_html/admin/includes/functions/sessions.php</b> on line <b>98</b><br />

thank :)

Share this post


Link to post
Share on other sites

That is usually caused by an extra line. If line 98 in that file is the last one then remove the last ?>. If it isn't, then it would depend on the code.

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

×