Rounding Prices in WooCommerce

I noticed a question today from Darren in the WooCommerce Community Facebook group. Darren’s question is: “I’m using a currency switcher based on where the visitor is located. My site is set in US dollars. Is there any snippet code to round up the product price to .99 for the other non US countries?”

A simple price rounding function can be implemented as follows:

<?php
/**
 * @snippet       Round all Prices
 * @url           https://davejesch.com/snippets/rounding-prices-in-woocommerce/
 * @author        Dave Jesch
 * @date-written  Apr 5, 2020
 * @testedwith    WooCommerce 4.0.x
 * @donate $5     https://davejesch.com/send-me-coffee/
 */
function d3j_round_price($price, $product = NULL)
{
	$price = floor($price) + .99;
	return $price;
}
add_filter('woocommerce_product_get_price', 'd3j_round_price', 990, 2);
add_filter('woocommerce_get_variation_regular_price', 'd3j_round_price', 99);
add_filter('woocommerce_get_variation_price', 'd3j_round_price', 99);

This code does a simple adjustment on the price, making the price always have a value ending in .99 cents. This filter should work, even with currency switcher plugins. Assuming of course, that the currency switchers use a priority less than 99 on their filters.

If you use this code, you can change the rounding method to end in any price value. If you want all prices to end in .97 cents instead of .99 cents, you can change the value added back to the price on line 12 to .97.

Leave a Reply

Your email address will not be published. Required fields are marked *