Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Created April 14, 2019 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/a9c29d598173e872589633387ae043ee to your computer and use it in GitHub Desktop.
Save damiencarbery/a9c29d598173e872589633387ae043ee to your computer and use it in GitHub Desktop.
Remove Zero Decimals from WooCommerce: Remove decimal portion of WooCommerce price if it is zero. https://www.damiencarbery.com/2019/04/remove-zero-decimals-from-wooCommerce/
<?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