How to Show Only 2 Decimals In Woocommerce Prices Using Php?

5 minutes read

To show only 2 decimals in WooCommerce prices using PHP, you can modify the price format by using the woocommerce_price_format filter. You can add the following code to your theme's functions.php file:

1
2
3
4
function custom_price_format( $format, $currency_pos ) {
    return str_replace( '%s', '%.2f', $format );
}
add_filter( 'woocommerce_price_format', 'custom_price_format', 10, 2 );


This code will display prices with 2 decimals instead of the default 4 decimals in WooCommerce. This will affect all prices across your site.

Best WooCommerce Cloud Hosting Providers of June 2024

1
Vultr

Rating is 5 out of 5

Vultr

  • Ultra-fast Intel Core
  • High Performance and Cheap Cloud Dedicated Servers
  • 1 click install Wordpress
  • Low Price and High Quality
2
Digital Ocean

Rating is 5 out of 5

Digital Ocean

  • Active Digital Community
  • Simple Control Panel
  • Starting from 5$ per month
3
AWS

Rating is 5 out of 5

AWS

4
Cloudways

Rating is 5 out of 5

Cloudways


How to display WooCommerce prices with only 2 decimal places using PHP?

To display WooCommerce prices with only 2 decimal places using PHP, you can use the following code snippet:

1
2
3
4
5
add_filter( 'woocommerce_price_trim_zeros', 'wc_trim_prices_decimal' );

function wc_trim_prices_decimal( $trim ) {
    return true;
}


You can add this code to your theme's functions.php file or in a custom plugin. This code will trim any zeros after the decimal point and display the prices with only 2 decimal places in WooCommerce.


How to edit the price format in WooCommerce to display only 2 decimal places with PHP?

To edit the price format in WooCommerce to display only 2 decimal places with PHP, you can use the following code snippet in your theme's functions.php file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
add_filter('woocommerce_price_trim_zeros', 'wc_price_trim_zeros');
function wc_price_trim_zeros($trim) {
    return true;
}

add_filter('raw_woocommerce_price', 'wc_raw_woocommerce_price', 100, 1);
function wc_raw_woocommerce_price($price) {
    return (float) $price;
}

add_filter('woocommerce_price_format', 'wc_price_format');
function wc_price_format($format) {
    $format = str_replace('%1$s', '%1$.2f', $format);
    return $format;
}


This code snippet modifies the WooCommerce price format to display only 2 decimal places. After adding this code to your functions.php file, the prices displayed on your WooCommerce store will be truncated to 2 decimal places.


How to change WooCommerce prices to show only two decimal digits using PHP?

You can change WooCommerce prices to show only two decimal digits using PHP by adding the following code snippet to your theme's functions.php file:

1
2
3
4
5
add_filter( 'woocommerce_price_format', 'custom_price_format' );

function custom_price_format( $format ) {
    return '%s%2$s';
}


This code snippet will override the default WooCommerce price format and force all prices to display with only two decimal digits.


What is the function to modify WooCommerce prices to show only two decimals?

You can use the following function to modify WooCommerce prices to show only two decimals:

1
2
3
4
function custom_price_format( $price ) {
    return number_format( $price, 2, '.', '' );
}
add_filter( 'woocommerce_price_format', 'custom_price_format' );


You can add this code to your theme's functions.php file or in a custom plugin to apply the changes. This function will format all prices displayed by WooCommerce to show two decimal places.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To call a WooCommerce class function from functions.php, you can use the global $woocommerce object. You can access the WooCommerce class functions by calling the global $woocommerce object and using the appropriate functions. For example, if you want to get t...
To show only the WooCommerce SKU number on your product pages, you can customize the WooCommerce template file for product pages. You would need to locate the product template file in your theme's directory (usually under /wp-content/themes/your-theme-name...
To round up the price in WooCommerce, you can use a plugin or add custom code to your theme's functions.php file. One popular plugin for rounding up prices in WooCommerce is the "WooCommerce Advanced Pricing" plugin. This plugin allows you to set u...