/** * Aelia Currency Switcher for WooCommerce - Return the price of a product in * a specific currency. * * HOW TO USE THIS CODE * Add the code to the bottom of your theme's functions.php file (see https://www.skyverge.com/blog/add-custom-code-to-wordpress/). * After doing so, call function aelia_get_product_price_in_currency() to get * the product price in a specific currency. * * Examples * -------- * In the following examples, "123" is a product ID. You can also pass an instance * of a product to the function. * - aelia_get_product_price_in_currency(123, 'EUR', 'incl'); // Get product price in EUR, inclusive of tax * - aelia_get_product_price_in_currency(123, 'EUR', 'excl'); // Get product price in EUR, exclusive of tax * - aelia_get_product_price_in_currency(123, 'USD', 'incl'); // Get product price in USD, inclusive of tax * - aelia_get_product_price_in_currency(123, 'USD', 'excl'); // Get product price in USD, exclusive of tax * * 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 */ /** * Returns a product price, inclusive or exclusive of tax, in a specific currency. * * @param WC_Product product * @param string currency * @param incl_tax * @return float */ function aelia_get_product_price_in_currency($product, $currency, $incl_tax) { if(is_numeric($product)) { $product = wc_get_product($product); } // Create a dynamic function that will allow to set the currency on the fly $currency_override_func = function($selected_currency) use ($currency) { return $currency; }; // Set the currency to the specified one add_filter('wc_aelia_cs_selected_currency', $currency_override_func, 10); // Get the price inclusive of exclusive of tax switch($incl_tax) { case 'incl': $price = wc_get_price_including_tax($product); break; case 'excl': $price = wc_get_price_excluding_tax($product); break; default: $price = $product->get_price(); } // Remove the custom filter, to restore the original currency, before // returning the price remove_filter('wc_aelia_cs_selected_currency', $currency_override_func, 10); return $price; }