Reputation: 409
I am struggling to find a solution to my price format issue.
If the price in cart adds up to $1 then it should show $1.00 Now, this works if I change the decimal in WC settings to "2".
However, if wanted it to show as 0.065 and 0.010 So I added a "3" in the WC decimal settings. So, how can I get the price to look like this:
I just want to remove the trailing zero if it is a zero.
So I have tried a few things:
add_filter( 'woocommerce_price_trim_zeros', 'wc_hide_trailing_zeros', 10, 1 );
function wc_hide_trailing_zeros( $trim ) {
return true;
}
This removes ALL zeros.
Then I tried a function that supposed to remove the last item in a str.
function rstrtrim($str, $remove=null)
{
$str = (string)$str;
$remove = (string)$remove;
if(empty($remove))
{
return rtrim($str);
}
$len = strlen($remove);
$offset = strlen($str)-$len;
while($offset > 0 && $offset == strpos($str, $remove, $offset))
{
$str = substr($str, 0, $offset);
$offset = strlen($str)-$len;
}
return rtrim($str);
}
//Remove last zero on a 3 decimal setting
echo rstrtrim((string)$number_format, '0');
But I think it's not working because it is reading it as a string and not an integer, dunno.
So, all I want to do is set my decimal settings in WC to "3" and remove the "last" integer IF it is a zero...
so this:
Would be this:
Upvotes: 0
Views: 757
Reputation: 409
Ok, I figured it out!
add_filter( 'formatted_woocommerce_price', 'wc_custom_price_format', 10, 5 );
function wc_custom_price_format( $number_format, $price, $decimals, $decimal_separator, $thousand_separator){
$lastnum = $number_format[strlen($number_format)-1];
if ($lastnum == 0):
return substr($number_format, 0, -1);
else:
return $number_format;
endif;
}
Upvotes: 1