• I am using the filter woocommerce_calculated_total to round up the final total on the checkout page.

    The trouble is there is a message beside the total that tells the user how much of the total is tax. But this is calculated before woocommerce_calculated_total is applied so is wrong. e.g. 23% tax rate

    Before rounding: 740.46 (includes €138.46 Tax)

    After rounding: 740 (includes €138.46 Tax)

    This is wrong. The correct tax rate should be (740 / 123 * 23) 138.37

    I cant seem to find another filter to change the total before tax is calculated.

    How can I fix this

Viewing 10 replies - 1 through 10 (of 10 total)
  • 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

    Thread Starter stupidchief

    (@stupidchief)

    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 ) );
    
    Thread Starter stupidchief

    (@stupidchief)

    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

    Thread Starter stupidchief

    (@stupidchief)

    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 );
    Thread Starter stupidchief

    (@stupidchief)

    Thanks for your time James.

    That doesn’t work either

    Thread Starter stupidchief

    (@stupidchief)

    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.

    splet99

    (@splet99)

    did you sort this out, I have a similar problem…

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘woocommerce rounding issue’ is closed to new replies.