Now Reading: How to remove trailing zeros from WooCommerce prices

Loading

How to remove trailing zeros from WooCommerce prices

svgSeptember 6, 2024WooCommerceWordPresszorca

When working with prices in WooCommerce, there are times when you might want to hide unnecessary zeros after the decimal point for rounded prices. For example, if the price of a product is 79.00, it would be preferable to display just 79. However, to handle this correctly, it’s important to consider WooCommerce settings like the number of decimal places and the decimal separator format.

Fortunately, WooCommerce has built-in functionality to remove zeros from rounded decimal numbers.

Configuring WooCommerce to remove zeros from prices

To make WooCommerce automatically remove trailing zeros from decimal numbers, you only need to add one line of code:

<?php
// If the price is rounded to a whole number, remove the trailing zeros and the decimal.
// For instance, $79.00 will display as $79
add_filter( 'woocommerce_price_trim_zeros', '__return_true' );

The add_filter function uses the callback function as the second argument, so a special function __return_true is used here that always returns true.

This filter ensures that WooCommerce will always strip the zeros and the decimal part for any rounded price, regardless of the price format configured, such as .00, .000, etc.

2 People voted this article. 2 Upvotes - 0 Downvotes.
svg

What do you think?

Show comments / Leave a comment

One Comment:

  • A WordPress Commenter

    September 6, 2024 / at 2:22 pmsvgReply

    Hi, this is a comment.
    To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.
    Commenter avatars come from Gravatar.

Leave a reply

Loading
svg
Quick Navigation
  • 01

    How to remove trailing zeros from WooCommerce prices