businessdad

Easy Digital Downl. Currency Switcher - Advanced Integration

Jan 21st, 2016
89
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Helper class to allow 3rd party plugins to implement multi-currency
  3.  * capabilities. The class was designed to work with the Aelia Currency Switcher,
  4.  * for Easy Digital Downloads but it's generic enough to be adapted to other solutions.
  5.  *
  6.  * @author Aelia <support@aelia.co>
  7.  * @link https://aelia.co
  8.  * @link https://aelia.co/shop/currency-switcher-for-easy-digital-downloads/
  9.  */
  10. class Currency_Helper_Class {
  11.   // @var string Shop's base currency. Used for caching.
  12.   protected static $_base_currency;
  13.  
  14.   /**
  15.    * Returns shop's base currency. This method implements some simple caching,
  16.    * to avoid calling get_option() too many times.
  17.    *
  18.    * @return string Shop's base currency.
  19.    */
  20.   protected static function shop_base_currency() {
  21.     if(empty(self::$_base_currency)) {
  22.       global $edd_options;
  23.       self::$_base_currency = $edd_options['currency'];
  24.     }
  25.     return self::$_base_currency;
  26.   }
  27.  
  28.   /**
  29.    * Returns the list of the currencies enabled in the Aelia Currency Switcher.
  30.    * If the Currency Switcher is not installed, or active, then the list will
  31.    * only contain the shop base currency.
  32.    *
  33.    * @return array An array of currency codes.
  34.    */
  35.   public static function enabled_currencies() {
  36.     return apply_filters('edd_aelia_cs_enabled_currencies', array(self::shop_base_currency()));
  37.   }
  38.  
  39.   /**
  40.    * Advanced integration with EDD Currency Switcher, developed by Aelia
  41.    * (http://aelia.co). This method can be used by any 3rd party plugin to
  42.    * return prices in the active currency. The method allows to specify prices
  43.    * explicitly, thus bypassing automatic FX conversion.
  44.    *
  45.    * @param double price The source price.
  46.    * @param array prices_per_currency An optional array of currency => value
  47.    * pairs. If an entry is found in this array that matches the $to_currency
  48.    * parameters, such value is taken as is, and the automatic conversion logic
  49.    * is skipped.
  50.    * @param string to_currency The target currency. If empty, the active currency
  51.    * is taken.
  52.    * @param string from_currency The source currency. If empty, EDD base
  53.    * currency is taken.
  54.    * @return double The price converted from source to destination currency.
  55.    */
  56.   public static function get_price_in_currency($price, array $prices_per_currency = array(), $to_currency = null, $from_currency = null) {
  57.     if(empty($from_currency)) {
  58.       $from_currency = self::shop_base_currency();
  59.     }
  60.     if(empty($to_currency)) {
  61.       $to_currency = edd_get_currency();
  62.     }
  63.  
  64.     // If an explicit price was passed for the target currency, just take it
  65.     if(!empty($prices_per_currency[$to_currency])) {
  66.       return $prices_per_currency[$to_currency];
  67.     }
  68.     return self::convert($price, $to_currency, $from_currency);
  69.   }
  70.  
  71.   /**
  72.    * Converts an amount from one currency to another, using exchange rates.
  73.    *
  74.    * @param float amount The amount to convert.
  75.    * @param string to_currency The destination currency.
  76.    * @param string from_currency The source currency.
  77.    * @return float The amount converted to the target destination currency.
  78.    */
  79.   public static function convert($amount, $to_currency, $from_currency = null) {
  80.     return apply_filters('edd_aelia_cs_convert', $amount, $from_currency, $to_currency);
  81.   }
  82.  
  83.   public function test() {
  84.     $price = 123;
  85.     // Just call the get_price_in_currency() method. This will use automatic conversion
  86.     return self::get_price_in_currency($price);
  87.   }
  88.  
  89.   public function test2() {
  90.     $prices_per_currency = array(
  91.       'USD' => 123,
  92.       'EUR' => 456,
  93.       'GBP' => 789,
  94.     );
  95.  
  96.     // Call the get_price_in_currency() method. This will take the explicit
  97.     // price defined in $prices_per_currency, if available. If not available,
  98.     // automatic conversion will be used instead. The examples below assume that
  99.     // Easy Digital Downloads base currency is USD
  100.  
  101.     // This is the price to convert, in USD
  102.     $price = 123;
  103.     // Example 1 - This will return "456", i.e. the price in EUR specified in the list, above
  104.     $converted_price = self::get_price_in_currency($price, $prices_per_currency, 'EUR');
  105.  
  106.     // Example 2 - This will return "789", i.e. the price in GBP specified in the list, above
  107.     $converted_price = self::get_price_in_currency($price, $prices_per_currency, 'GBP');
  108.  
  109.     // Example 3 - This will return "166.29" (exchange rate as of 04/08/2015), because there
  110.     // isn't an explicity AUD price in $prices_per_currency list
  111.     $converted_price = self::get_price_in_currency($price, $prices_per_currency, 'AUD');
  112.   }
  113. }
RAW Paste Data