Here’s another fun thing I found recently. It’s definitely a bug, and I do have a solution, but I don’t know if it’s a good one. I’ve been told that these rounding errors are common in e-commerce systems, so there’s got to be a more general solution.
Here’s the setup: a product worth $29.95; a coupon taking 10% off the cart amount; number of decimal places set to 2.
Add one product and the coupon, and here’s what you end up with: a $26.96 cart total, with a $3.00 discount amount. The database has the correct amounts for the line items ($26.955 and $2.995 respectively) but because of how rounding works, you don’t get to see that. PHP by default rounds to the closest value, and .5 will always round up. Even if I set it to always round up or down, the values would still be off by $0.01.
Here’s my solution: adding a function to action ‘woocommerce_after_calculate_totals’. If there are percentage-type discounts, check if the subtotal and total discount discount amount are off. If they are, adjust one of the percentage discounts (if there are more than one). It works, but I’m not happy with it. It seems there should be something better…