SHOW:
|
|
- or go back to the newest paste.
| 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 | - | * but it's generic enough to be adapted to other solutions. |
| 4 | + | * for Easy Digital Downloads but it's generic enough to be adapted to other solutions. |
| 5 | * | |
| 6 | - | * Need a consultation? Find us on Codeable: https://bit.ly/aelia_codeable |
| 6 | + | |
| 7 | - | * |
| 7 | + | |
| 8 | * @link https://aelia.co/shop/currency-switcher-for-easy-digital-downloads/ | |
| 9 | */ | |
| 10 | - | * @link https://aelia.co/shop/currency-switcher-woocommerce/ |
| 10 | + | class Currency_Helper_Class {
|
| 11 | // @var string Shop's base currency. Used for caching. | |
| 12 | - | class Advanced_CurrencySwitcher_Integration {
|
| 12 | + | |
| 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 | - | self::$_base_currency = get_option('woocommerce_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 | - | return apply_filters('wc_aelia_cs_enabled_currencies', array(self::shop_base_currency()));
|
| 37 | + | |
| 38 | ||
| 39 | /** | |
| 40 | * Advanced integration with EDD Currency Switcher, developed by Aelia | |
| 41 | - | * Returns a product's base currency. A product's base currency is the point |
| 41 | + | * (http://aelia.co). This method can be used by any 3rd party plugin to |
| 42 | - | * of reference to calculate other prices, and it can differ from shop's base |
| 42 | + | * return prices in the active currency. The method allows to specify prices |
| 43 | - | * currency. |
| 43 | + | * explicitly, thus bypassing automatic FX conversion. |
| 44 | - | * For example, if a shop's base currency is USD, a product's base currency |
| 44 | + | |
| 45 | - | * can be EUR. In such case, product prices in other currencies can be |
| 45 | + | |
| 46 | - | * calculated automatically, as long as the EUR one is entered. |
| 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 | - | * @param int product_id A product ID. |
| 48 | + | * parameters, such value is taken as is, and the automatic conversion logic |
| 49 | - | * @param string default_currency The default currency to use if the product |
| 49 | + | * is skipped. |
| 50 | - | * doesn't have a base currency. |
| 50 | + | |
| 51 | - | * @return string A currency code. |
| 51 | + | |
| 52 | * @param string from_currency The source currency. If empty, EDD base | |
| 53 | - | public static function get_product_base_currency($product_id, $default_currency = null) {
|
| 53 | + | |
| 54 | - | if(empty($default_currency)) {
|
| 54 | + | |
| 55 | - | $default_currency = self::shop_base_currency(); |
| 55 | + | |
| 56 | public static function get_price_in_currency($price, array $prices_per_currency = array(), $to_currency = null, $from_currency = null) {
| |
| 57 | - | return apply_filters('wc_aelia_cs_product_base_currency', $default_currency, $product_id);
|
| 57 | + | |
| 58 | $from_currency = self::shop_base_currency(); | |
| 59 | } | |
| 60 | if(empty($to_currency)) {
| |
| 61 | - | * Advanced integration with WooCommerce Currency Switcher, developed by Aelia |
| 61 | + | $to_currency = edd_get_currency(); |
| 62 | - | * (https://aelia.co). This method can be used by any 3rd party plugin to |
| 62 | + | |
| 63 | - | * return prices in the active currency. |
| 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 | - | * @param string from_currency The source currency. If empty, WooCommerce base |
| 68 | + | |
| 69 | } | |
| 70 | ||
| 71 | /** | |
| 72 | - | public static function get_price_in_currency($price, $to_currency = null, $from_currency = null) {
|
| 72 | + | |
| 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 | - | $to_currency = get_woocommerce_currency(); |
| 77 | + | |
| 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 | - | return apply_filters('wc_aelia_cs_convert', $amount, $from_currency, $to_currency);
|
| 92 | + | 'EUR' => 456, |
| 93 | 'GBP' => 789, | |
| 94 | - | |
| 94 | + | ); |
| 95 | - | /** |
| 95 | + | |
| 96 | - | * Returns a list of prices that should be a applied to a product for each currency. |
| 96 | + | // Call the get_price_in_currency() method. This will take the explicit |
| 97 | - | * This is a example of how the fixed-price logic could be implemented. The |
| 97 | + | // price defined in $prices_per_currency, if available. If not available, |
| 98 | - | * actual implementation depends on the specifics of the product being proceseed, |
| 98 | + | // automatic conversion will be used instead. The examples below assume that |
| 99 | - | * on how the prices were stored, and so on. |
| 99 | + | // Easy Digital Downloads base currency is USD |
| 100 | - | * |
| 100 | + | |
| 101 | - | * @param WC_Product product The product for which the prices should be retrieved. |
| 101 | + | // This is the price to convert, in USD |
| 102 | - | * @param string currency The currency for which the product prices will be retrieved. |
| 102 | + | |
| 103 | - | * @return array An array of prices. |
| 103 | + | // Example 1 - This will return "456", i.e. the price in EUR specified in the list, above |
| 104 | - | */ |
| 104 | + | $converted_price = self::get_price_in_currency($price, $prices_per_currency, 'EUR'); |
| 105 | - | protected function get_product_prices_for_currency($product, $currency) {
|
| 105 | + | |
| 106 | - | // Load the custom prices in the specified currency |
| 106 | + | // Example 2 - This will return "789", i.e. the price in GBP specified in the list, above |
| 107 | - | $prices = array( |
| 107 | + | $converted_price = self::get_price_in_currency($price, $prices_per_currency, 'GBP'); |
| 108 | - | 'my_custom_price' => get_post_meta($product->id, '_my_custom_price_' . $currency, true), |
| 108 | + | |
| 109 | - | 'my_other_custom_price' => get_post_meta($product->id, 'my_other_custom_price_' . $currency, true), |
| 109 | + | // Example 3 - This will return "166.29" (exchange rate as of 04/08/2015), because there |
| 110 | - | ); |
| 110 | + | // isn't an explicity AUD price in $prices_per_currency list |
| 111 | - | return $prices; |
| 111 | + | $converted_price = self::get_price_in_currency($price, $prices_per_currency, 'AUD'); |
| 112 | - | } |
| 112 | + | |
| 113 | } |