Skip to content

Instantly share code, notes, and snippets.

@acki
Last active November 18, 2020 22:42
Show Gist options
  • Save acki/be09035618db0a341ba6f2238798fb62 to your computer and use it in GitHub Desktop.
Save acki/be09035618db0a341ba6f2238798fb62 to your computer and use it in GitHub Desktop.
Dirty fix for WooCommerce rounding problems due to non-round tax values
<?php
/*
* WooCommerce dirty rounding fix for special tax values (7.7 in Switzerland)
* Rounding to 0.05 instead of 0.01
* created by Christoph S. Ackermann
* https://www.cubetech.ch
* 05.01.2018
*/
add_filter( 'woocommerce_get_price_excluding_tax', 'round_price_product', 10, 1 );
add_filter( 'woocommerce_get_price_including_tax', 'round_price_product', 10, 1 );
add_filter( 'woocommerce_tax_round', 'round_price_product', 10, 1);
add_filter( 'woocommerce_get_price', 'round_price_product', 10, 1);
add_filter( 'woocommerce_calculated_total', 'round_price_product', 10, 1);
add_filter( 'woocommerce_calculated_subtotal', 'round_price_product', 10, 1);
add_filter( 'woocommerce_cart_subtotal', 'round_subtotal', 10, 3);
function round_subtotal( $cart_subtotal, $compound, $instance ) {
$origValue = $cart_subtotal;
preg_match( '/\d+\.\d+/', $origValue, $floatValue);
$roundedValue = number_format( round_price_product( $floatValue[0] ), 2 );
$returnValue = str_replace( $floatValue, $roundedValue, $origValue );
return $returnValue;
}
function round_price_product( $price ){
// Return rounded price
return round( $price * 2, 1 ) / 2;
}
@Hameedhudeen
Copy link

Hi!
Is there an update to the script?
I applied this to my latest woocommerce site and it notified me that "woocommerce_get_price" has been deprecated and changed to "woocommerce_product_get_price".

And the script didn't seem to take effect even after I changed the syntax.
It would be nice if we can have an update :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment