Your above filter (which returns true
) triggers the execution of wc_trim_zeros()
function, which indeed removes zeros only if the price has only 0 decimals.
What you need is to use the formatted_woocommerce_price
hook instead.
The below filter will cut all trailing zeroes:
add_filter('formatted_woocommerce_price', function($formatted_price, $price, $decimals, $decimal_separator) {
// Need to trim 0s only if we have the decimal separator present.
if (strpos($formatted_price, $decimal_separator) !== false) {
$formatted_price = rtrim($formatted_price, '0');
// After trimming trailing 0s, it may happen that the decimal separator will remain there trailing... just get rid of it, if it's the case.
$formatted_price = rtrim($formatted_price, $decimal_separator);
}
return $formatted_price;
}, 10, 4);
UPDATE: the below code will only cut a trailing zero if it's the 3rd and last decimal:
add_filter('formatted_woocommerce_price', function($formatted_price, $price, $decimals, $decimal_separator) {
// Need to trim 0s only if we have the decimal separator present.
if (strpos($formatted_price, $decimal_separator) !== false) {
$formatted_price = preg_replace('/^(\d+' . preg_quote($decimal_separator, '/' ) . '\d{2})0$/', "$1", $formatted_price);
}
return $formatted_price;
}, 10, 4);
Disclaimer: code not tried out, written directly in the answer box.