-
-
Save acki/be09035618db0a341ba6f2238798fb62 to your computer and use it in GitHub Desktop.
<?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; | |
} |
Hello Christoph,
I'am using your helpful code to round prices. It's work fine, many thanks.
But I'am experiencing a strange behavior which I am not able to understand and fix, so I give a try to know if you could give to me some help or information. In short, I can't have the woocommerce_cart_item_subtotal (if I'm not wrong for hook's name) rounded the same as the woocommerce_cart_subtotal. You can find an attached picture to see in concrete what I'am describing. Red square 2 displays the price rounded but not the red square 1. I googled a lot but so far did not find any solution. Many, many thanks for your precious help.
Thank you and nice day.
Olivier.
@klloll and @B-Olivier sorry - didn't saw your comments until today.
Still an issue?
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 :)
Hello,
Thank you sooo much for this code!
It's work fine!
I've a question: in the basket, everything goes well except the line: (including CHF 1.76 VAT).
Do you know why?
Thank you have a nice day