Remove Zero Decimals in Product Price

In this blog post you will learn how to remove decimals in general and how to remove them when they are zero on the Woocommerce products.

 

Remove Decimals in the Woocommerce Settings

If you want to remove the decimals in the whole shop, you can simply do this by going to WooCommerce –> Settings and set the “Number of decimals” to zero.

 

Remove Decimals with Code

To change the price, we use the filter “formatted_woocommerce_price”. The price with the decimals and the price without the decimals are compared (with the function intval()). If this results in zero, a new number without decimals is created, but the thousands separator is respected. There is a part in the code that does not remove the decimals for Single Product Pages if they are zero. You can remove this part if you want to remove the zero decimals for the Single Product Pages as well.

add_filter( 'formatted_woocommerce_price', 'arrow_design_remove_zero_decimals', 10, 5 );
function arrow_design_remove_zero_decimals( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
/*
// Leave decimals in on single product page.
if ( is_product() ) {
return $formatted_price;
}
*/

if ( $price - intval( $price ) == 0 ) {
// Format units, including thousands separator if necessary.
return $unit = number_format( intval( $price ), 0, $decimal_separator, $thousand_separator );
}
else {
return $formatted_price;
}
}

Before and After

This is how the shop prices look like before adding the code:

 

When the code is added and saved, it looks like this:

Conclusion

We hope this Remove Zero Decimals in Product Price was helpful and you are able to integrate this on your own website! You can find more articles here:

 

Read another Woocommerce post : ‘Top Woocommerce Code Hooks and Filters

 

Arrow Design, based in Dublin, Ireland, provides quality website design services in Dublin and beyond at affordable prices. If you would like help with implementing the above code, or any wordpress website development project, contact us. We love website design and it shows! We provide custom wordpress plugin development, website design training and lots more.

We do it all, so you don’t have to!

Related Posts

Woocommerce Account Page Hook Guide

In this article you will find a visual hook guide for the Woocommerce Account Pages, like the Login/Register page, the Downloads page or the Orders page.

Woocommerce PHP – Product Categories by Product ID

Woocommerce Single Product Page Hook Guide

In this article you will find a visual hook guide for the Woocommerce Single Product Page. This should help you to quickly and easily find the hook positions on the page.

Woocommerce PHP – Product Categories by Product ID

Woocommerce PHP – Product Categories by Product ID

In this tutorial you will learn how to check if a product is assigned to a tag, a category or a custom taxonomy. You can check if a product is is on the shop page, in the cart, in an order & more.

…We do more, so you can do less 🙂

 

Leave a message and we will be straight back to you!

Pin It on Pinterest

Share This