Advertisement
businessdad

Currency Switcher for WooCommerce - Flat Rate per Country

Feb 26th, 2016
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.59 KB | None | 0 0
  1. // Add this code to your theme's functions.php to ensure that the free
  2. // shipping thresholds used by the Flat Rate Shipping per Country plugin
  3. // are converted correctly.
  4.  
  5. /**
  6.  * Fix for the Flat Rate per Country/Region plugin.
  7.  * Processes the shipping method settings, ensuring that the free shipping
  8.  * thresholds are converted to the active currency.
  9.  *
  10.  * @param array shipping_methods The shipping methods to process.
  11.  * @return array The array of shipping methods, with the Flat Rate per Country
  12.  * plugin's settings converted as needed.
  13.  * @author Aelia <support@aelia.co>
  14.  * @link https://aelia.co
  15.  */
  16. add_filter('woocommerce_shipping_methods', function($shipping_methods) {
  17.     foreach($shipping_methods as $key => $method) {
  18.         if(!is_object($method) && ($method == 'WC_Flat_Rate_Per_Country_Region')) {
  19.             $method = new $method();
  20.         }
  21.         if($method instanceof WC_Flat_Rate_Per_Country_Region) {
  22.             $settings_to_convert = array('world_free_above');
  23.  
  24.             // Build a list of the settings whose value should be converted to the
  25.             // active currency
  26.             $counters = array('region', 'country', 'state');
  27.             foreach($counters as $counter_id) {
  28.                 for($idx = 0; $idx < (int)$method->settings['per_' . $counter_id . '_count']; $idx++) {
  29.                     // The shipping plugin uses counters that start from 1
  30.                     $settings_to_convert[] = 'per_' . $counter_id . '_' . ($idx + 1) . '_fr';
  31.                 }
  32.             }
  33.  
  34.             foreach($settings_to_convert as $setting_key) {
  35.                 if(!empty($method->settings[$setting_key])) {
  36.                     $method->settings[$setting_key] = aelia_convert($method->settings[$setting_key]);
  37.                 }
  38.             }
  39.  
  40.             $shipping_methods[$key] = $method;
  41.         }
  42.     }
  43.     return $shipping_methods;
  44. }, 51);
  45.  
  46.  
  47. /**
  48.  * Basic integration with WooCommerce Currency Switcher, developed by Aelia
  49.  * (http://aelia.co). This method can be used by any 3rd party plugin to
  50.  * return prices converted to the active currency.
  51.  *
  52.  * @param double price The source price.
  53.  * @param string to_currency The target currency. If empty, the active currency
  54.  * will be taken.
  55.  * @param string from_currency The source currency. If empty, WooCommerce base
  56.  * currency will be taken.
  57.  * @return double The price converted from source to destination currency.
  58.  * @author Aelia <support@aelia.co>
  59.  * @link https://aelia.co
  60.  */
  61. function aelia_convert($price, $to_currency = null, $from_currency = null) {
  62.     if(empty($from_currency)) {
  63.         $from_currency = get_option('woocommerce_currency');
  64.     }
  65.     if(empty($to_currency)) {
  66.         $to_currency = get_woocommerce_currency();
  67.     }
  68.     return apply_filters('wc_aelia_cs_convert', $price, $from_currency, $to_currency);
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement