ServiceCurrencyConversion by Ryan Cramer

Provides API capabilities for converting from one currency to another, using the OpenExchangeRates service.

Currency Conversion for ProcessWire

This module is designed for performing currency conversions among ~165 world currencies. It uses OpenExchangeRates.org (or compatible) for data so that currency exchange rates are always up-to-date.

It provides various API functions that you can use to convert from one currency to another. This is especially handy for generating rate tables in multiple currencies or giving users of your site the option to see prices in their currency.

For a live example of a basic currency conversion tool built with this module see the included convert.php file and test it out here.

How to install


  1. Copy the files here into /site/modules/ServiceCurrencyConversion/

  2. In your ProcessWire admin, check for new modules and click install for this module.

  3. Obtain an OpenExchangeRates.org API key. Because this module caches the exchange rate data for a period that you specify, you may find the free account to be adequate unless you need up-to-the-minute exchange rate data.


  4. Paste your key into the module configuration screen where prompted to do so. After saving, you should see a table indicating current exchange rate data.

How to use


Usage is best demonstrated by example. Here is a basic example that demonstrates conversion of a rate from USD (US Dollars) to EUR (Euros). The context for these examples is from one of your site template files.

$cc = $modules->get('ServiceCurrencyConversion'); 
$dollars = 100; // amount of currency we want to convert
$euros = $cc->convert('USD', 'EUR', $dollars);
echo "<p>$dollars US Dollars equals $euros Euros</p>";

USD and EUR can be any currency codes known by OpenExchangeRates.org, meaning you should be able to convert between any two currencies that you want to.

For a live example of a currency conversion tool, try out the included convert.php file included with this module. Copy it to your /site/templates/, add it as a new template, and create a page with it.

API


The following methods are provided by the Service Exchange Rates module. All of the example calls below assume you have a copy of the ServiceCurrencyConversion module in the variable $ex, obtained by a call like this:

$cc = $modules->get('ServiceCurrencyConversion'); 

$cc->convert($fromCurrency, $toCurrency, $amount)

Convert an amount from one currency to another and return the converted amount. For $fromCurrency and $toCurrency, specify the 3-digit currency code.

// how many dollars are there in 100 euros?
echo $cc->convert('EUR', 'USD', 100); // outputs 136.335 (when I tested)

$cc->getSymbol($currency)

Return the currency symbol used by the given currency code.

echo $cc->getSymbol('USD'); // outputs "$"

$cc->getName($currency)

Return the currency name (in English) for the given currency code.

echo $cc->getName('EUR'); // outputs "Euro"

$cc->getNames()

Return array of all possible currency names (in English) indexed by currency code.

$names = $cc->getNames(); 

$cc->getCodes()

Return array of all possible currency codes (3 digits each)

$codes = $cc->getCodes(); 

$cc->lastUpdated()

Returns the time (UNIX timestamp) that the exchange rate data was last updated.

$time = $cc->lastUpdated();
echo "<p>Last updated: " . date('Y-m-d H:i:s', $time) . "</p>";

$cc->updateNow()

Force the exchange rates to update now.

$success = $cc->updateNow();
if($success) echo "Rates were updated";

$cc->getRatesTable($useCache = true)

Return an array with all exchange rate data indexed by currency code with each item containing the currency name, symbol and USD exchange rate

$data = $cc->getRatesTable($useCache = true); 
print_r($data);

The above would output:

array(
  'USD' => array(
    'code' => 'USD',
    'name' => 'US Dollars',
    'symbol' => '$',
    'x' => 1 // all exchange rates USD
  ),
  'EUR' => array(
    // ...
  ),
  // ... and so on for all currencies
);

The getRatesTable method accepts a $useCache method, which is true by default. If set to false, it will retrieve the data from OpenExchangeRates.org immediately without considering a previously retrieved local copy of rates (cache).


$cc->getConvertedRatesTable($fromCode = 'USD', $amount = 1)

Get exchange rates from one currency to all others. Returns an array of all data indexed by currency code with each item containing the currency name, symbol, exchange rate (from USD), and converted amount.

// how much is 5 Euros in all other currencies?
$data = $cc->getConvertedRatesTable('EUR', 5);
print_r($data);

The above would output:

array(
  'CAD' => array(
    'code' => 'CAD',
    'name' => 'Canadian Dollar',
    'symbol' => '$',
    'x' => 1.08459 // USD exchange rate
    'amount' => 7.39332209033
  ),
  // ... and so on for all currencies
)

Install and use modules at your own risk. Always have a site and database backup before installing new modules.

Twitter updates

  • ProcessWire 3.0.185 (dev) core updates, plus new Session Allow module— More
    17 September 2021
  • Three new ProcessWire Textformatter modules: Find/Replace, Markdown in Markup, and Emoji— More
    3 September 2021
  • This week we have a new master version released after a year in the making. With nearly 40 pull requests, hundreds of new additions and more than 100 issue reports resolved, this new version has a ton of great new stuff— More
    27 August 2021

Latest news

  • ProcessWire Weekly #384
    In the 384th issue of ProcessWire Weekly we'll cover the latest core updates, introduce a new module called Session Allow, and highlight a new site of the week. Read on!
    Weekly.pw / 18 September 2021
  • ProcessWire 3.0.184 new master/main version
    This week we have a new master/main version released after a full year in the making. As you might imagine, this new version has a ton of great new stuff and we’ll try to cover much of it here.
    Blog / 27 August 2021
  • Subscribe to weekly ProcessWire news

“The end client and designer love the ease at which they can update the website. Training beyond how to log in wasn’t even necessary since ProcessWire’s default interface is straightforward.” —Jonathan Lahijani