How to remove trailing zeros from round WooCommerce prices

When dealing with a combination of round numbers and decimals for product prices on a WooCommerce store. Often, it can be desirable to hide the trailing .00 when a price is a round number. This is easily achievable through some string replacement but this can also go wrong if don’t factor in configurable parts of WooCommerce such as the number of decimal places and the decimals separator.

Fortunately, WooCommerce has functionality built right in to strip zeros from rounded decimal numbers.

Configuring WooCommerce to remove zeros from round decimals

With a single line of code, we can tell WooCommerce to always trim zeros from a price if the price has them:

<?php
// If a WooCommerce price is a full dollar value, trim the
// decimal & any trailing zeros.
// e.g; $49.00 will display as just $49
add_filter( 'woocommerce_price_trim_zeros', '__return_true' );

By using this filter, WooCommerce will always attempt to strip zeros and the decimal from any price that is a round number. It’ll handle any configured price format such as .00, .000, .00000000, ,00 etc.

Leveraging WooCommerce to remove zeros from any price

There may be times where you have a price assigned in a variable and need to strip any zeros. For this, we can just use WooCommerce’s wc_trim_zeros( $price ) function:

<?php
// Need to trim zeros from a price on the fly?
$price = 49.00; // Can be numeric or a string.
$price = wc_trim_zeros( $price ); // === '49'

About the author

Phil Kurth is a web developer living in Melbourne, Australia. Phil has a long history of WordPress development and enjoys building tools to empower others in their web design/development practice.

When not working with the web, Phil is usually spending time with his two young sons or is out hiking through the Australian bush.

Good dev stuff, delivered.

Product news, tips, and other cool things we make.

We never share your data. Read our Privacy Policy

© 2022 Hookturn Digital. All rights reserved.