/**
* Helper class to allow 3rd party plugins to implement multi-currency
* capabilities. The class was designed to work with the Aelia Currency Switcher,
* for Easy Digital Downloads but it's generic enough to be adapted to other solutions.
*
* @author Aelia <[email protected]>
* @link https://aelia.co
* @link https://aelia.co/shop/currency-switcher-for-easy-digital-downloads/
*/
class Currency_Helper_Class {
// @var string Shop's base currency. Used for caching.
protected static $_base_currency;
/**
* Returns shop's base currency. This method implements some simple caching,
* to avoid calling get_option() too many times.
*
* @return string Shop's base currency.
*/
protected static function shop_base_currency() {
if(empty(self::$_base_currency)) {
global $edd_options;
self::$_base_currency = $edd_options['currency'];
}
return self::$_base_currency;
}
/**
* Returns the list of the currencies enabled in the Aelia Currency Switcher.
* If the Currency Switcher is not installed, or active, then the list will
* only contain the shop base currency.
*
* @return array An array of currency codes.
*/
public static function enabled_currencies() {
return apply_filters('edd_aelia_cs_enabled_currencies', array(self::shop_base_currency()));
}
/**
* Advanced integration with EDD Currency Switcher, developed by Aelia
* (http://aelia.co). This method can be used by any 3rd party plugin to
* return prices in the active currency. The method allows to specify prices
* explicitly, thus bypassing automatic FX conversion.
*
* @param double price The source price.
* @param array prices_per_currency An optional array of currency => value
* pairs. If an entry is found in this array that matches the $to_currency
* parameters, such value is taken as is, and the automatic conversion logic
* is skipped.
* @param string to_currency The target currency. If empty, the active currency
* is taken.
* @param string from_currency The source currency. If empty, EDD base
* currency is taken.
* @return double The price converted from source to destination currency.
*/
public static function get_price_in_currency($price, array $prices_per_currency = array(), $to_currency = null, $from_currency = null) {
if(empty($from_currency)) {
$from_currency = self::shop_base_currency();
}
if(empty($to_currency)) {
$to_currency = edd_get_currency();
}
// If an explicit price was passed for the target currency, just take it
if(!empty($prices_per_currency[$to_currency])) {
return $prices_per_currency[$to_currency];
}
return self::convert($price, $to_currency, $from_currency);
}
/**
* Converts an amount from one currency to another, using exchange rates.
*
* @param float amount The amount to convert.
* @param string to_currency The destination currency.
* @param string from_currency The source currency.
* @return float The amount converted to the target destination currency.
*/
public static function convert($amount, $to_currency, $from_currency = null) {
return apply_filters('edd_aelia_cs_convert', $amount, $from_currency, $to_currency);
}
public function test() {
$price = 123;
// Just call the get_price_in_currency() method. This will use automatic conversion
return self::get_price_in_currency($price);
}
public function test2() {
$prices_per_currency = array(
'USD' => 123,
'EUR' => 456,
'GBP' => 789,
);
// Call the get_price_in_currency() method. This will take the explicit
// price defined in $prices_per_currency, if available. If not available,
// automatic conversion will be used instead. The examples below assume that
// Easy Digital Downloads base currency is USD
// This is the price to convert, in USD
$price = 123;
// Example 1 - This will return "456", i.e. the price in EUR specified in the list, above
$converted_price = self::get_price_in_currency($price, $prices_per_currency, 'EUR');
// Example 2 - This will return "789", i.e. the price in GBP specified in the list, above
$converted_price = self::get_price_in_currency($price, $prices_per_currency, 'GBP');
// Example 3 - This will return "166.29" (exchange rate as of 04/08/2015), because there
// isn't an explicity AUD price in $prices_per_currency list
$converted_price = self::get_price_in_currency($price, $prices_per_currency, 'AUD');
}
}
Comments