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. To use the code, simply place it in your theme's functions.php. See How to Safely Add Custom Code to WordPress Sites for more information on how to do that.


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.


If you wish to display rounded prices, and you also have to show them inclusive of tax, an easy way to do so is to show the price exclusive of tax (which is rounded), followed by the one inclusive of tax. A filter for the woocommerce_get_price_html hook could be used for that purpose.


Example 1 - Rounding product prices

Write a filter for hook wc_aelia_cs_convert_product_price to round product prices (not shipping prices or coupons). Please note that this filter was added in Currency Switcher 3.8.14.151214, it's not available in previous versions.

/**
 * 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 WC_Product The product whose price is being converted.
 * @param string price_type The price being converted (e.g. regular_price,
 * sale_price, signup_fee, etc).
 * @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. 
   *
   * IMPORTANT
   * You will have to add one of these formulas, or your custom formula, to 
   * perform the rounding. If you just copy this code without a formula, prices
   * will be returned as they are, without any rounding.
   *
   * 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('wc_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).

Example 2 - Rounding any amount converted by the Currency Switcher

Write a filter for hook wc_aelia_cs_converted_amount to round any amount that was converted by the Currency Switcher.

/**
 * 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 decimal<span class="fr-marker" data-id="0" data-type="false" style="display: none; line-height: 0;"></span><span class="fr-marker" data-id="0" data-type="true" style="display: none; line-height: 0;"></span>s configured for the target currency.
 * @return float The rounded amount.
 * @author Aelia <support@aelia.co>
 */
function my_custom_rounding($converted_amount, $original_amount, $from_currency, $to_currency, $decimals) {
  // 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
   *
   * IMPORTANT
   * You will have to add one of these formulas, or your custom formula, to 
   * perform the rounding. If you just copy this code without a formula, prices
   * will be returned as they are, without any rounding.
   *
   * 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('wc_aelia_cs_converted_amount', 'my_custom_rounding', 10, 5);

  

You can purchase the Currency Switcher from our online shop.