Advertisement
businessdad

Currency Switcher - Get a product price in currency

Dec 20th, 2017
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.82 KB | None | 0 0
  1. /**
  2.  * Aelia Currency Switcher for WooCommerce - Return the price of a product in
  3.  * a specific currency.
  4.  *
  5.  * HOW TO USE THIS CODE
  6.  * Add the code to the bottom of your theme's functions.php file (see https://www.skyverge.com/blog/add-custom-code-to-wordpress/).
  7.  * After doing so, call function aelia_get_product_price_in_currency() to get
  8.  * the product price in a specific currency.
  9.  *
  10.  * Examples
  11.  * --------
  12.  * In the following examples, "123" is a product ID. You can also pass an instance
  13.  * of a product to the function.
  14.  * - aelia_get_product_price_in_currency(123, 'EUR', 'incl'); // Get product price in EUR, inclusive of tax
  15.  * - aelia_get_product_price_in_currency(123, 'EUR', 'excl'); // Get product price in EUR, exclusive of tax
  16.  * - aelia_get_product_price_in_currency(123, 'USD', 'incl'); // Get product price in USD, inclusive of tax
  17.  * - aelia_get_product_price_in_currency(123, 'USD', 'excl'); // Get product price in USD, exclusive of tax
  18.  *
  19.  * GPL DISCLAIMER
  20.  * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
  21.  * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
  22.  * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
  23.  * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
  24.  * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
  25.  *
  26.  * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
  27.  */
  28.  
  29. /**
  30.  * Returns a product price, inclusive or exclusive of tax, in a specific currency.
  31.  *
  32.  * @param WC_Product product
  33.  * @param string currency
  34.  * @param incl_tax
  35.  * @return float
  36.  */
  37. function aelia_get_product_price_in_currency($product, $currency, $incl_tax) {
  38.   if(is_numeric($product)) {
  39.     $product = wc_get_product($product);
  40.   }
  41.  
  42.   // Create a dynamic function that will allow to set the currency on the fly
  43.   $currency_override_func = function($selected_currency) use ($currency) {
  44.     return $currency;
  45.   };
  46.  
  47.   // Set the currency to the specified one
  48.   add_filter('wc_aelia_cs_selected_currency', $currency_override_func, 10);
  49.  
  50.   // Get the price inclusive of exclusive of tax
  51.   switch($incl_tax) {
  52.     case 'incl':
  53.       $price = wc_get_price_including_tax($product);
  54.       break;
  55.     case 'excl':
  56.       $price = wc_get_price_excluding_tax($product);
  57.       break;
  58.     default:
  59.       $price = $product->get_price();
  60.   }
  61.  
  62.   // Remove the custom filter, to restore the original currency, before
  63.   // returning the price
  64.   remove_filter('wc_aelia_cs_selected_currency', $currency_override_func, 10);
  65.   return $price;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement