Advertisement
businessdad

WooCommerce Currency Switcher - Advanced integration example

Jun 5th, 2015
1,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.91 KB | None | 0 0
  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.
  5.  *
  6.  * Need a consultation? Find us on Codeable: https://bit.ly/aelia_codeable
  7.  *
  8.  * @author Aelia <support@aelia.co>
  9.  * @link https://aelia.co
  10.  * @link https://aelia.co/shop/currency-switcher-woocommerce/
  11.  */
  12. class Advanced_CurrencySwitcher_Integration  {
  13.   // @var string Shop's base currency. Used for caching.
  14.   protected static $_base_currency;
  15.  
  16.   /**
  17.    * Returns shop's base currency. This method implements some simple caching,
  18.    * to avoid calling get_option() too many times.
  19.    *
  20.    * @return string Shop's base currency.
  21.    */
  22.   protected static function shop_base_currency() {
  23.     if(empty(self::$_base_currency)) {
  24.       self::$_base_currency = get_option('woocommerce_currency');
  25.     }
  26.     return self::$_base_currency;
  27.   }
  28.  
  29.   /**
  30.    * Returns the list of the currencies enabled in the Aelia Currency Switcher.
  31.    * If the Currency Switcher is not installed, or active, then the list will
  32.    * only contain the shop base currency.
  33.    *
  34.    * @return array An array of currency codes.
  35.    */
  36.   public static function enabled_currencies() {
  37.     return apply_filters('wc_aelia_cs_enabled_currencies', array(self::shop_base_currency()));
  38.   }
  39.  
  40.   /**
  41.    * Returns a product's base currency. A product's base currency is the point
  42.    * of reference to calculate other prices, and it can differ from shop's base
  43.    * currency.
  44.    * For example, if a shop's base currency is USD, a product's base currency
  45.    * can be EUR. In such case, product prices in other currencies can be
  46.    * calculated automatically, as long as the EUR one is entered.
  47.    *
  48.    * @param int product_id A product ID.
  49.    * @param string default_currency The default currency to use if the product
  50.    * doesn't have a base currency.
  51.    * @return string A currency code.
  52.    */
  53.   public static function get_product_base_currency($product_id, $default_currency = null) {
  54.     if(empty($default_currency)) {
  55.       $default_currency = self::shop_base_currency();
  56.     }
  57.     return apply_filters('wc_aelia_cs_product_base_currency', $default_currency, $product_id);
  58.   }
  59.  
  60.   /**
  61.    * Advanced integration with WooCommerce Currency Switcher, developed by Aelia
  62.    * (https://aelia.co). This method can be used by any 3rd party plugin to
  63.    * return prices in the active currency.
  64.    *
  65.    * @param double price The source price.
  66.    * @param string to_currency The target currency. If empty, the active currency
  67.    * is taken.
  68.    * @param string from_currency The source currency. If empty, WooCommerce base
  69.    * currency is taken.
  70.    * @return double The price converted from source to destination currency.
  71.    */
  72.   public static function get_price_in_currency($price, $to_currency = null, $from_currency = null) {
  73.     if(empty($from_currency)) {
  74.       $from_currency = self::shop_base_currency();
  75.     }
  76.     if(empty($to_currency)) {
  77.       $to_currency = get_woocommerce_currency();
  78.     }
  79.  
  80.     return self::convert($price, $to_currency, $from_currency);
  81.   }
  82.  
  83.   /**
  84.    * Converts an amount from one currency to another, using exchange rates.
  85.    *
  86.    * @param float amount The amount to convert.
  87.    * @param string to_currency The destination currency.
  88.    * @param string from_currency The source currency.
  89.    * @return float The amount converted to the target destination currency.
  90.    */
  91.   public static function convert($amount, $to_currency, $from_currency = null) {
  92.     return apply_filters('wc_aelia_cs_convert', $amount, $from_currency, $to_currency);
  93.   }
  94.    
  95.     /**
  96.      * Returns a list of prices that should be a applied to a product for each currency.
  97.      * This is a example of how the fixed-price logic could be implemented. The
  98.      * actual implementation depends on the specifics of the product being proceseed,
  99.      * on how the prices were stored, and so on.
  100.      *
  101.      * @param WC_Product product The product for which the prices should be retrieved.
  102.      * @param string currency The currency for which the product prices will be retrieved.
  103.      * @return array An array of prices.
  104.      */
  105.     protected function get_product_prices_for_currency($product, $currency) {
  106.         // Load the custom prices in the specified currency        
  107.         $prices = array(
  108.             'my_custom_price' => get_post_meta($product->id, '_my_custom_price_' . $currency, true),
  109.             'my_other_custom_price' => get_post_meta($product->id, 'my_other_custom_price_' . $currency, true),
  110.         );
  111.         return $prices;
  112.     }
  113.  
  114.     /**
  115.      * Performs a simple test conversion of an arbitrary value, using exchange
  116.      * rates.
  117.      */
  118.   public function test() {
  119.     $price = 123;
  120.     // Just call the get_price_in_currency() method. This will use automatic conversion
  121.     return self::get_price_in_currency($price);
  122.   }
  123.  
  124.     /**
  125.      * Alter the callbacks used by the Currency Switcher, replacing the callback
  126.      * for a "my_custom_product" type with a custon one.
  127.      *
  128.      * @param callable convert_callback The original callback.
  129.      * @param WC_Product product The callback being converted.
  130.      * @return callable The callback that will convert the product's prices.
  131.      */
  132.     public function wc_aelia_currencyswitcher_product_convert_callback($convert_callback, $product) {
  133.         if($product->product_type === 'my_custom_product') {
  134.             $convert_callback = array($this, 'convert_my_custom_product');
  135.         }
  136.        
  137.         return $convert_callback;
  138.     }
  139.    
  140.     /**
  141.      * Converts the prices of a custom product to the specified currency.
  142.      *
  143.      * @param WC_Product product A variable product.
  144.      * @param string currency A currency code.
  145.      * @return WC_Product The product with converted prices.
  146.      */
  147.     public function convert_my_custom_product($product, $currency) {
  148.         /* Here you can implement your custom logic to convert the prices.
  149.          *  The two lines below are just an example. Here you can do anything you
  150.          *  want with the product, to ensure that its prices are converted correctly.
  151.          */
  152.  
  153.         // Load the product prices that were entered manually for the specified currency
  154.         $product_prices_for_currency = $this->get_product_prices_for_currency($product, $currency);
  155.        
  156.         // If prices were entered manually, then they will be used as they are. If not,
  157.         // product's price in base currency will be converted using exchange rates
  158.         $product->my_custom_price = isset($product_prices_for_currency['my_custom_price']) ? $product_prices_for_currency['my_custom_price'] : self::get_price_in_currency($product->my_custom_price);
  159.         $product->my_other_custom_price = isset($product_prices_for_currency['my_other_custom_price']) ? $product_prices_for_currency['my_other_custom_price'] : self::get_price_in_currency($product->my_other_custom_price);
  160.        
  161.         return $product;
  162.     }  
  163.  
  164.     public function __construct() {
  165.         add_filter('wc_aelia_currencyswitcher_product_convert_callback', array($this, 'wc_aelia_currencyswitcher_product_convert_callback'), 10, 2);
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement