Advertisement
businessdad

WooCommerce Currency Switcher - Basic integration example

Jun 5th, 2015
1,713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.87 KB | None | 0 0
  1. class Basic_CurrencySwitcher_Integration {
  2.   /**
  3.    * Basic integration with WooCommerce Currency Switcher, developed by Aelia
  4.    * (http://aelia.co). This method can be used by any 3rd party plugin to
  5.    * return prices converted to the active currency.
  6.    *
  7.    * Need a consultation? Find us on Codeable: https://aelia.co/hire_us
  8.    *
  9.    * @param double price The source price.
  10.    * @param string to_currency The target currency. If empty, the active currency
  11.    * will be taken.
  12.    * @param string from_currency The source currency. If empty, WooCommerce base
  13.    * currency will be taken.
  14.    * @return double The price converted from source to destination currency.
  15.    * @author Aelia <support@aelia.co>
  16.    * @link https://aelia.co
  17.    */
  18.   protected function get_price_in_currency($price, $to_currency = null, $from_currency = null) {
  19.     // If source currency is not specified, take the shop's base currency as a default
  20.     if(empty($from_currency)) {
  21.       $from_currency = get_option('woocommerce_currency');
  22.     }
  23.     // If target currency is not specified, take the active currency as a default.
  24.     // The Currency Switcher sets this currency automatically, based on the context. Other
  25.     // plugins can also override it, based on their own custom criteria, by implementing
  26.     // a filter for the "woocommerce_currency" hook.
  27.     //
  28.     // For example, a subscription plugin may decide that the active currency is the one
  29.     // taken from a previous subscription, because it's processing a renewal, and such
  30.     // renewal should keep the original prices, in the original currency.
  31.     if(empty($to_currency)) {
  32.       $to_currency = get_woocommerce_currency();
  33.     }
  34.  
  35.     // Call the currency conversion filter. Using a filter allows for loose coupling. If the
  36.     // Aelia Currency Switcher is not installed, the filter call will return the original
  37.     // amount, without any conversion being performed. Your plugin won't even need to know if
  38.     // the multi-currency plugin is installed or active
  39.     return apply_filters('wc_aelia_cs_convert', $price, $from_currency, $to_currency);
  40.   }
  41.    
  42.     /**
  43.      * Performs a simple test conversion.
  44.      */
  45.   public function test_conversion() {
  46.     $price = 123;
  47.     // Just call the get_price_in_currency() method, no further action required
  48.     return $this->get_price_in_currency($price);
  49.   }
  50.    
  51.     public function __construct() {
  52.         add_filter('wc_aelia_currencyswitcher_product_convert_callback', array($this, 'wc_aelia_currencyswitcher_product_convert_callback'), 10, 2);
  53.     }
  54.  
  55.     /**
  56.      * Alter the callbacks used by the Currency Switcher, replacing the callback
  57.      * for a "my_custom_product" type with a custon one.
  58.      *
  59.      * @param callable convert_callback The original callback.
  60.      * @param WC_Product product The callback being converted.
  61.      * @return callable The callback that will convert the product's prices.
  62.      */
  63.     public function wc_aelia_currencyswitcher_product_convert_callback($convert_callback, $product) {
  64.         if($product->product_type === 'my_custom_product') {
  65.             $convert_callback = array($this, 'convert_my_custom_product');
  66.         }
  67.        
  68.         return $convert_callback;
  69.     }
  70.    
  71.     /**
  72.      * Converts the prices of a custom product to the specified currency.
  73.      *
  74.      * @param WC_Product product A variable product.
  75.      * @param string currency A currency code.
  76.      * @return WC_Product The product with converted prices.
  77.      */
  78.     public function convert_my_custom_product($product, $currency) {
  79.         /* Here you can implement your custom logic to convert the prices.
  80.          *  The two lines below are just an example. Here you can do anything you
  81.          *  want with the product, to ensure that its prices are converted correctly.
  82.          */
  83.         $product->my_custom_price = $this->get_price_in_currency($product->my_custom_price);
  84.         $product->my_other_custom_price = $this->get_price_in_currency($product->my_other_custom_price);
  85.        
  86.         return $product;
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement