businessdad icon

WooCommerce Checkout Addons - Convert prices to currency

businessdad | PRO | 10/30/17 04:32:57 PM UTC | 0 ⭐ | 1202 👁️ | Never ⏰ | []
PHP |

3.11 KB

|

None

|

0 👍

/

0 👎

/**
 * WooCommerce Checkout Addons - Convert addon prices using the Aelia Currency Switcher.
 *
 * HOW TO USE THIS CODE
 * Simply add the code to the bottom of your theme's functions.php file, and it
 * will run automatically. For more information: https://www.skyverge.com/blog/add-custom-code-to-wordpress/
 *
 * GPL DISCLAIMER
 * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
 * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
 * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
 * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
 * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
 *
 * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
 */
 
 
/**
 * Converts a price from a source to a target currency.
 *
 * @param double price The source price.
 * @param string to_currency The target currency. If empty, the active currency
 * will be taken.
 * @param string from_currency The source currency. If empty, WooCommerce base
 * currency will be taken.
 * @return double The price converted from source to destination currency.
 * @author Aelia <[email protected]>
 * @link https://aelia.co
 */
function aelia_get_price_in_currency($price, $to_currency = null, $from_currency = null) {
  // If source currency is not specified, take the shop's base currency as a default
  if(empty($from_currency)) {
    $from_currency = get_option('woocommerce_currency');
  }
  // If target currency is not specified, take the active currency as a default.
  // The Currency Switcher sets this currency automatically, based on the context. Other
  // plugins can also override it, based on their own custom criteria, by implementing
  // a filter for the "woocommerce_currency" hook.
  //
  // For example, a subscription plugin may decide that the active currency is the one
  // taken from a previous subscription, because it's processing a renewal, and such
  // renewal should keep the original prices, in the original currency.
  if(empty($to_currency)) {
    $to_currency = get_woocommerce_currency();
  }
  
  // Call the currency conversion filter. Using a filter allows for loose coupling. If the
  // Aelia Currency Switcher is not installed, the filter call will return the original
  // amount, without any conversion being performed. Your plugin won't even need to know if
  // the multi-currency plugin is installed or active
  return apply_filters('wc_aelia_cs_convert', $price, $from_currency, $to_currency);
}
 
/**
 * Converts the price of a checkout addon to the active currency.
 *
 * @param float cost
 * @return float
 */ 
function aelia_convert_checkout_addon_cost($cost) {
  return aelia_get_price_in_currency($cost);
}
add_filter('wc_checkout_add_ons_add_on_get_cost', 'aelia_convert_checkout_addon_cost', 10, 1);

Comments