Advertisement
businessdad

WooCommerce Checkout Addons - Convert prices to currency

Oct 30th, 2017
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.11 KB | None | 0 0
  1. /**
  2.  * WooCommerce Checkout Addons - Convert addon prices using the Aelia Currency Switcher.
  3.  *
  4.  * HOW TO USE THIS CODE
  5.  * Simply add the code to the bottom of your theme's functions.php file, and it
  6.  * will run automatically. For more information: https://www.skyverge.com/blog/add-custom-code-to-wordpress/
  7.  *
  8.  * GPL DISCLAIMER
  9.  * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
  10.  * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
  11.  * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
  12.  * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
  13.  * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
  14.  *
  15.  * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
  16.  */
  17.  
  18.  
  19. /**
  20.  * Converts a price from a source to a target currency.
  21.  *
  22.  * @param double price The source price.
  23.  * @param string to_currency The target currency. If empty, the active currency
  24.  * will be taken.
  25.  * @param string from_currency The source currency. If empty, WooCommerce base
  26.  * currency will be taken.
  27.  * @return double The price converted from source to destination currency.
  28.  * @author Aelia <support@aelia.co>
  29.  * @link https://aelia.co
  30.  */
  31. function aelia_get_price_in_currency($price, $to_currency = null, $from_currency = null) {
  32.   // If source currency is not specified, take the shop's base currency as a default
  33.   if(empty($from_currency)) {
  34.     $from_currency = get_option('woocommerce_currency');
  35.   }
  36.   // If target currency is not specified, take the active currency as a default.
  37.   // The Currency Switcher sets this currency automatically, based on the context. Other
  38.   // plugins can also override it, based on their own custom criteria, by implementing
  39.   // a filter for the "woocommerce_currency" hook.
  40.   //
  41.   // For example, a subscription plugin may decide that the active currency is the one
  42.   // taken from a previous subscription, because it's processing a renewal, and such
  43.   // renewal should keep the original prices, in the original currency.
  44.   if(empty($to_currency)) {
  45.     $to_currency = get_woocommerce_currency();
  46.   }
  47.  
  48.   // Call the currency conversion filter. Using a filter allows for loose coupling. If the
  49.   // Aelia Currency Switcher is not installed, the filter call will return the original
  50.   // amount, without any conversion being performed. Your plugin won't even need to know if
  51.   // the multi-currency plugin is installed or active
  52.   return apply_filters('wc_aelia_cs_convert', $price, $from_currency, $to_currency);
  53. }
  54.  
  55. /**
  56.  * Converts the price of a checkout addon to the active currency.
  57.  *
  58.  * @param float cost
  59.  * @return float
  60.  */
  61. function aelia_convert_checkout_addon_cost($cost) {
  62.   return aelia_get_price_in_currency($cost);
  63. }
  64. add_filter('wc_checkout_add_ons_add_on_get_cost', 'aelia_convert_checkout_addon_cost', 10, 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement