Jump to content



Photo
- - - - -

Auto Update Currencies


  • Please log in to reply
102 replies to this topic

#101   Peper

Peper
  • Members
  • 398 posts
  • Real Name:Pierre
  • Gender:Male
  • Location:South Africa

Posted 14 December 2015 - 06:57

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?


Contributions successfully installed : Header tags SEO, Scrolling new products, Customer testimonials, Support tickets, Polls, Link exchange, SPPC, X-sell, Master products, Dhtml menu, Image cache, Slide show, Product videos, Product custom sort, Product notes, Discount coupons, Ask question(review style + admin), CP builder, Customers also purchased, price list, // SEO URLS 5, and forgot the others and then a real admin setup.

#102   Jack_mcs

Jack_mcs
  • Members
  • 27,724 posts
  • Real Name:Jack York
  • Gender:Male
  • Location:Michigan

Posted 14 December 2015 - 13:42

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.



#103   Bob Terveuren

Bob Terveuren
  • Members
  • 486 posts
  • Real Name:Bob Terveuren
  • Gender:Male

Posted 14 December 2015 - 19:10

Hi Peper - dirty trick? Moi?  :D