How to Show Trailing Zeros on Prices in WooCommerce: A Step-By-Step Guide

Home » Snippets » How to Show Trailing Zeros on Prices in WooCommerce: A Step-By-Step Guide
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

If you’re running an online store with WooCommerce, you may have noticed that it doesn’t display trailing zeros in prices by default. This means that if a product costs, say, $40.00, WooCommerce will display it as $40. While this might not seem like a big issue, it can actually make your eCommerce site appear less professional. Don’t worry, though: showing trailing zeros in WooCommerce is straightforward, and in this article, we will guide you on how to do just that.

					add_filter( 'woocommerce_price_trim_zeros', 'wpturbo_price_trim_zeros', 10, 1 );
function wpturbo_price_trim_zeros($trim) {
    // set $trim to false to prevent WooCommerce from trimming zeros
    return false;
}
				

Our code snippet is rather straightforward.

We start by utilizing the add_filter function, which is used to hook a custom function to a specific filter action. In our case, the function being hooked is wpturbo_price_trim_zeros and it’s attached to the 'woocommerce_price_trim_zeros' filter action.

add_filter( 'woocommerce_price_trim_zeros', 'wpturbo_price_trim_zeros', 10, 1 );

The ‘woocommerce_price_trim_zeros’ is a filter provided by WooCommerce, which determines whether trailing zeros should be trimmed from prices or not. By default, WooCommerce trims the trailing zeros.

Following to the filter name is the custom function name wpturbo_price_trim_zeros. The third argument is the priority at which this function will execute. In this case, it is 10. The fourth and last argument is the number of arguments the function accepts, which is 1 in our case.

Next, we define the function wpturbo_price_trim_zeros, which takes in a single parameter $trim.

function wpturbo_price_trim_zeros($trim) {
    // set $trim to false to prevent WooCommerce from trimming zeros
    return false;
}

Here, the function wpturbo_price_trim_zeros simply returns false. This overrides the default behavior of the ‘woocommerce_price_trim_zeros’ filter. The filter works by checking the value returned by the functions attached to it. If any function returns false, it prevents WooCommerce from trimming trailing zeros from prices.

Hence, the end result is that trailing zeros will be shown on prices in WooCommerce, contrary to the default behavior.

Register an account to save your snippets or go Pro to get more features.