WooCommerce: Adding tax to old (completed) orders

I have a little problem. A client of ours setup a WooCommerce shop himself and forgot to add taxes. Since he was in over his head he decided to get help setting it all up.

Now unfortunately this shop is already live and has about 50 completed orders in it. I setup the taxes etc. correctly for him but this will work only for new orders, not the old ones.

Read More

Does anybody know a simple way of adding the taxes to the old orders (all prices are including tax), maybe a loop or something I can run over the orders to get the taxes calculated?

I tried something really simple, just constructing the order and calling calculate_taxes() but this will only add a tax of 0,00€ to the order, so I guess I’m missing something important here:

$order = new WC_Order(1234);
$order->calculate_taxes(); 

Thank you for your help

Related posts

2 comments

  1. What I did was:
    1. update the product to pending payment.
    2. recalculate the tax, change status to complete again, and set order action to send invoice to customer

    that’s it.

  2. Alright, I dove really deep into the the mechanics behind this, so I think I can answer this myself, but be warned, it’s more complicated then it seems.

    There are some important things about how WooCommerce handles orders before though:

    • Shipping costs have to entered without taxes
    • Prices are stored without taxes in an order, even when you added them including taxes to the product

    So when you add taxes after there are completed orders, that leaves you with the following problems:

    • The prices need to be changed in the old orders
    • The shippings costs need to be chached in the old orders

    In more technical words, you would have to loop through all orders, loop through the items in the order, update prices, add the tax, update the shipping costs and only then can you run “calculate_taxes()” as I did in my really simple example.

    I can’t provide you with a finished function that does all this (yet) since I first have to clarify if the client is willing to pay for this. I’ll add the function if he does. If anybody stumbles upon this and writes this function fell free to post is as an answer, I’ll accept it as correct.

Comments are closed.