About Currency Conversion
Provides API capabilities for converting from one currency to another, using the OpenExchangeRates service.
|Category 1||Development Tools|
Modules that have more to do with assisting development of a site rather than managing its content.
|Category 2||Language Modules|
Modules involved in providing multi-language support.
|Category 3||Social, Feeds, Services|
Modules that provide or work with social networking services and/or external feeds.
Close to stable, but users are advised to be cautious and test thoroughly.*
|Compatibility||2.4, 2.5, 2.6, 2.7|
|Date Added||June 1, 2014|
This module's files should be placed in /site/modules/ServiceCurrencyConversion/
How to install or uninstall modules
It provides various API functions that you can use to convert from onecurrency 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.
Copy the files here into /site/modules/ServiceCurrencyConversion/
In your ProcessWire admin, check for new modules and click install for this module.
Obtain an OpenExchangeRates.org API key. Because this module caches the exchange rate datafor a period that you specify, you may find the free account to be adequate unless you need up-to-the-minute exchange rate data.
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.
Usage is best demonstrated by example. Here is a basic example thatdemonstrates 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, meaningyou 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.phpfile included with this module. Copy it to your /site/templates/, add it as a new template, and create a page with it.
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');
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)
Return the currency symbol used by the given currency code.
echo $cc->getSymbol('USD'); // outputs "$"
Return the currency name (in English) for the given currency code.
echo $cc->getName('EUR'); // outputs "Euro"
Return array of all possible currency names (in English) indexed by currency code.
$names = $cc->getNames();
Return array of all possible currency codes (3 digits each)
$codes = $cc->getCodes();
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>";
Force the exchange rates to update now.
$success = $cc->updateNow(); if($success) echo "Rates were updated";
Return an array with all exchange rate data indexed by currency codewith 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).
Get exchange rates from one currency to all others. Returns an array of alldata 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 )
No comments yet. Be the first to post!
Your e-mail is kept confidential and not included with your comment. Website is optional.
- Admin Helpers
- Core Modules
- Development Tools
- Field Types
- File Validator
- Input Fields
- Language Modules
- Language Packs
- Markup Generation
- Premium Modules
- Process Modules
- Proof of Concept
- Site Profiles
- Social, Feeds, Services
- Text Formatters
- Other Modules
- Users and Access
*Use modules at your own risk. There are no guarantees or warranties. It is recommended that you backup your site and database before installing new modules.