One of the stores I manage is currently facing a bug that is not solved yet by WooCommerce, regarding rounding prices introduced without tax. So far one of the temp fixes that work is changing the whole store to 4 decimals to improve accuracy. Now the problem is that some new clients seem to believe our prices are in another currency other than USD because of the 6 digits that get presented (19.998). I'm wondering if there is a way to format the output of woocommerce_get_price_html using something like number_format. I'm a bit lost because the output of this is escaped html to style the price, so something like this
add_filter( 'woocommerce_get_price_html', 'rounded_price_html', 100, 2 );
function rounded_price_html( $price, $product ){
return number_format( $price, 2, ',', '.');
}
would not work. So basically what I'm asking is, is there any way I can format the price of this function without interfering with the HTML? This should only affect the front end as we don't care about the four digits in the back. Creating something using JS is not an option as our theme is AJAX based and that would just create more work
One of the stores I manage is currently facing a bug that is not solved yet by WooCommerce, regarding rounding prices introduced without tax. So far one of the temp fixes that work is changing the whole store to 4 decimals to improve accuracy. Now the problem is that some new clients seem to believe our prices are in another currency other than USD because of the 6 digits that get presented (19.998). I'm wondering if there is a way to format the output of woocommerce_get_price_html using something like number_format. I'm a bit lost because the output of this is escaped html to style the price, so something like this
add_filter( 'woocommerce_get_price_html', 'rounded_price_html', 100, 2 );
function rounded_price_html( $price, $product ){
return number_format( $price, 2, ',', '.');
}
would not work. So basically what I'm asking is, is there any way I can format the price of this function without interfering with the HTML? This should only affect the front end as we don't care about the four digits in the back. Creating something using JS is not an option as our theme is AJAX based and that would just create more work
print the number only with two decimal numbers.
$backend_price = 2.3456;
echo sprintf('%.2f', $backend_price);
Even if you set the number of decimals in the backend you can still explicitly override it via wp_get_price_decimals
filter hook.
/**
* Return the number of decimals after the decimal point.
*
* @params int $decimals The number of decimals. Defaults to get_option( 'woocommerce_price_num_decimals', 2 )
* @return int
*/
function wp123_custom_decimals( $decimals ) {
return 2; // The number of decimals.
}
add_filter( 'wc_get_price_decimals', 'wp123_custom_decimals', 10, 1 );
Set the store to 3 decimal places and then use this:
add_filter( 'formatted_woocommerce_price', 'ts_woo_decimal_price', 10, 5 );
function ts_woo_decimal_price( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
$unit = number_format( intval( $price ), 0, $decimal_separator, $thousand_separator );
$three_dp = $price - intval( $price );
$two_dp = ( number_format($three_dp,2) * 100);
if($two_dp == 100) {
$two_dp = 0;
$unit++;
}
$decimal = sprintf( '%02d', ( $two_dp ) );
return $unit . '.' . $decimal ;
}