Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove Zero Decimals from WooCommerce: Remove decimal portion of WooCommerce price if it is zero.
<?php
/*
Plugin Name: Remove Zero Decimals from WooCommerce
Plugin URI: Plugin URI: https://www.damiencarbery.com/
Description: Remove decimal portion of WooCommerce price if it is zero.
Author: Damien Carbery
Author URI: https://www.damiencarbery.com
Version: 0.1
*/
add_filter( 'formatted_woocommerce_price', 'dcwd_remove_zero_decimals', 10, 5 );
function dcwd_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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.