Have you debugged and checked the value coming into your filter?
Based on what your saying is calculated AFTER rounding, it looks like you might have your decimal setting set to 0, meaning round to the nearest whole vs round to the nearest 100th (2)
WooCommerce > Settings > General :: Currency Options -> Number of Decimals
Number of decimals in woocommerce is set to 2
The value coming into the filter is 740.46
using the woocommerce_calculated_total does not update the tax value.
In the /wp-content/plugins/woocommerce/includes/class-wc-cart.php around line 1401, have you tried dumping out the variables that are going into the calculations?
// Grand Total - Discounted product prices, discounted tax, shipping cost + tax
var_dump($this->cart_contents_total);
var_dump($this->tax_total);
var_dump($this->shipping_tax_total);
var_dump($this->shipping_total);
var_dump($this->fee_total);
var_dump(( $this->cart_contents_total + $this->tax_total + $this->shipping_tax_total + $this->shipping_total + $this->fee_total ));
var_dump(round( $this->cart_contents_total + $this->tax_total + $this->shipping_tax_total + $this->shipping_total + $this->fee_total, $this->dp ));
$this->total = max( 0, apply_filters( 'woocommerce_calculated_total', round( $this->cart_contents_total + $this->tax_total + $this->shipping_tax_total + $this->shipping_total + $this->fee_total, $this->dp ), $this ) );
var_dump($this->cart_contents_total);
float(525)
var_dump($this->tax_total);
float(120.75)
var_dump($this->shipping_tax_total);
float(17.71)
var_dump($this->shipping_total);
float(77)
var_dump($this->fee_total);
int(0)
var_dump(( $this->cart_contents_total + $this->tax_total + $this->shipping_tax_total + $this->shipping_total + $this->fee_total ));
float(740.46)
var_dump(round( $this->cart_contents_total + $this->tax_total + $this->shipping_tax_total + $this->shipping_total + $this->fee_total, $this->dp ));
float(740.46)
So with all of that data, it looks like the calculations are correct – don’t you think?
Your formula (740 / 123 * 23) 138.37 is correct, for 740
But if you replace the 740 with the actual number, 740.46,
(740.46 / 123 * 23) you get 138.46
So if you round the TOTAL before the round() on line 1401, then you would get your 138.37
You are exactly right.
But how do you do that rounding? what filter can you use.
woocommerce_calculated_total does it after.
If I was able to round the shipping + shipping tax that would also work I think.
I basically need to round everything.
BUT the final tax must be 23% and MUST NOT be rounded. This is because in my country the tax people do not let you round VAT.
It is a complete pain in the neck and is the reason I don’t simply use 0 decimals
There is a action hook just before totals get rounded, you could hook into woocommerce_calculate_totals
// Allow plugins to hook and alter totals before final total is calculated
do_action( 'woocommerce_calculate_totals', $this );
Thanks for your time James.
That doesn’t work either
To reiterate the issue:
The final total displayed is:
740.46 (includes €138.46 Tax)
I then apply the filter woocommerce_calculated_total to round the total
Now what is displayed is:
740 (includes €138.46 Tax)
As you can see the tax does not change and this is obviously incorrect.
did you sort this out, I have a similar problem…