-3

I have an issue with the shipping tax calculation. I add the shipping cost without tax, and everything is calculated OK in the cart/checkout page. Also, in back office, the order is displayed correctly (cost/taxes are calculated OK).

The problem happens when you change the order status to Complete and the invoice is sent by email - it will automatically change the shipping cost and the total cost - it adds 0.01 (Note: if you manually generate the invoice before changing the order status to Complete, the invoice is generated OK - taxes/costs are OK in both invoice and back office).

Below are some screenshots with the current settings and the results: General OptionsTax Options Standard RatesShipping Flat RateCart taxes/costs are calculated correctly Back office taxes/costs are calculated correctly

Now, when you change the order status to Complete, it will send the invoice by email and will change the taxes/costs:Order Complete - incorrect taxes/costs

And this is how the amounts are displayed in the invoice:Invoice - incorrect taxes/costs

The shipping cost is 15 with tax included, so I have to configure it to 12.6050 (without tax) in order be calculated correctly: 15 lei.

I think the issue is related with the number of decimals used - WooCommerce is set to 2 decimals. If I change the WooCommerce setting to use 4 decimals, it is OK, but the amounts look awful and are very confusing for customers.

From accounting perspective, the shipping cost without tax should be 12.61 lei, and the shipping tax should be 2.39 lei (total shipping cost with tax included = 15 lei)

Using WooCommerce 3.0.5 and WooCommerce PDF Invoices & Packing Slips (tried with other invoicing plugins, and the issue is the same).

Do you have any ideas what should I do to use 2 decimals and keep the shipping cost to 15 lei (tax included)?

Thank you for your help.

2
+50

This isn't really a wordpress issue, but I'm going to answer it since I think the nature of the issue is not understanding that the code behind what is seen as an error in the outcome, is doing precisely what would make sense if the values entered were needed to create a total rather than reconstruct one. Simply put, it's a rounding issue, and it happens because the code is expecting (forcing) real-world values at certain stages of its calculations.

The Issue

When rounding numbers that were derived from a calculation, like in your case:

[ Gross / (tax rate +1) = pre tax], the results can be unexpected.

12.605042016806723 x 1.19 versus 12.6050 x 1.19 versus 12.60 x 1.19 versus 12.61 x 1.19, especially if the resulting value will be rounded again.

I think the issue is related with the number of decimals used - WooCommerce is set to 2 decimals. If I change the WooCommerce setting to use 4 decimals, it is OK, but the amounts look awful and are very confusing for customers.

I think you're right. And the real issue is that the resulting value of shipping + (shipping x tax rate) will also be rounded to two decimals.

119% of 12.6050 = 14.99995 (rounded to two decimals: 15.00)

119% of 12.61 (which would be the rounded value if system is using two decimals) = 15.0059 (rounded to two decimals: 15.01)

I don't know all the plugin settings you mentioned, but perhaps the

Round tax at subtotal level

option you're showing is rounding the shipping price to 12.61 before that tax calculation (aka, at subtotal). Regardless, there's a simple fix:

Solution:

Since you're trying to make it total 15.00, rather than accurately reflect shipping + shipping tax, drop everything after the second decimal place on shipping cost so it is 12.60 rather than 12.6050 or 12.605042016806723 ;)

12.60 x 1.19 = 14.994, which will round to 15.00.

  • Thank you for your answer @hwl. I did try to use 12.60, but the cost is not rounded to 15.00 - it will display 14.99 - see screenshot here: goo.gl/wVwP0n. I also tried with "Round at subtotal level" enabled and disabled. – Nicu Zecheru Jun 5 '17 at 12:28
  • I am also using this in my functions.php file to hide the trailing zeros: add_filter( 'woocommerce_price_trim_zeros', '__return_true' ); . However, this doesn't seem to affect the way the prices are rounded. – Nicu Zecheru Jun 5 '17 at 12:42
  • hmm... I would have to look deeper into how/where that rounding is done. You said woo does it correctly, but the invoice plugin for woo does not, right? Sounds like woo is dropping everything after two decimal places (15.0059), and the plugin is rounding it up (15.01), but by math rules of >5 gets rounded up, <5 rounded down. Woo's method (probably the correct one here) is saying that .0059 = .00. That's a guess though since I don't know what gets sent to plugin (numbers to calculate, or complete sums). But I suspect its rounding method is the culprit. – hwl Jun 5 '17 at 15:45
  • I use woocommerce-pdf-invoice plugin. I double checked and the rounding happens when you change the order status (not only for order complete, when the invoice is generated). My current workaround is to use 4 decimals in order to calculate the shipping cost correctly. To display amounts with 2 decimals in the invoice, I changed the woo-pdf-invoice.class.php and hardcoded the number of decimals to 2: //$decimals = is_null($decimals) ? and changed it to $decimals = '2'; This way the price is displayed correctly with 2 decimals (only the tax is displayed with 4 decimals) – Nicu Zecheru Jun 5 '17 at 17:16
  • I have another Woocommerce installation which is OK - using 2 decimals and the rounding is OK. I will have to re-check current plugins and see what is causing woocommerce to re-calculate totals when changing the order status. Thank you again for your help, I will mark this as solved so you can get the bounty. – Nicu Zecheru Jun 5 '17 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.