The code in this article is to be considered a customisation. As such, it's outside the scope of our support service. Should you need assistance implementing the code, please feel free to contact us to request a consultation.


The Currency Switcher calculates currency conversions mathematically, applying a simple mathematical rounding which uses the amount of decimals specified in Currency Switcher Options page. If you wish to round product prices to a specific figure, for example to the closest 0.5, you can add a filter, which will perform some calculation depending on the result you would like to achieve.


The examples you find below show how you can round prices in several ways. You should add the custom code to your theme's functions.php file, or to a custom plugin.


Note

The prices are rounded before taxes are applied to them. This is because rounding a tax-inclusive price would involve a recalculation of both the original price and the tax applied to it.

Example 1 - Rounding product prices

Write a filter for hook edd_aelia_cs_convert_product_price to round product prices (not shipping prices or coupons). Please note that this filter was added in Currency Switcher 1.4.11.170710, it's not available in previous versions.
<?php
/**
 * Rounds product prices to an arbitrary precision.
 *
 * @param float converted_amount The amount calculated by the Currency Switcher.
 * @param float original_amount The original amount, before conversion.
 * @param string from_currency The source currency.
 * @param string to_currency The target currency.
 * @param int decimals The amount of decimals configured for the target currency.
 * @param EDD_Product The product whose price is being converted.
 * @return float The rounded amount.
 * @author Aelia <support@aelia.co>
 */
function my_custom_rounding($converted_amount, $original_amount, $from_currency, $to_currency, $decimals, $product, $price_type) {
  // If the amount is not numeric, then it cannot be converted reliably. Assuming 
  // that it's "zero" would be incorrect
  if(!is_numeric($converted_amount)) {
    return $converted_amount;
  }

  /* Sample roundings
   *
   * Round to closest 0.5
   * $converted_amount = round($converted_amount * 2, 0) / 2;
   *
   * Round price so that it ends with .99
   * $converted_amount = round($converted_amount, 0) - 0.01;
   *
   * Round price to nearest 5
   * $converted_amount = round($converted_amount / 5, 0) * 5;
   */
  return $converted_amount;
}
add_filter('edd_aelia_cs_convert_product_price', 'my_custom_rounding', 10, 7);

Notes

  • Prices that you enter manually in product configuration will be taken as they are (i.e. they will not be processed by the filter). 

You can purchase the Currency Switcher from our online shop.