WooCommerce Recalculate Order, Taxes - Delete & Add Order_item + discount

Viewed 56

I would like to briefly give you some information about our problem here: We need to correct all our Woocommerce orders from 2021 and now we are facing a big challenge. Since we have changed the tax class on the products, we can't just have the order recalculated but need to re-add the entire order line item to the order and remove the old line item.

We have now found a workaround that works:

  • Edit corresponding purchase order
  • Set purchase order from "completed" to "on hold
  • Update/Save
  • Now the order can be edited:
  • Check which coupon has been redeemed
  • Remember the product ID of the order
  • Delete product completely
  • Add new product via product ID
  • If voucher was used Add voucher code
  • Update/Save

This is exactly the process we use to correct our orders as needed. From this we have programmed a bot that goes through this process in wp-admin. Unfortunately, with tens of thousands of orders, this takes months....

Now I have come across Hooks and Functions and believe there is a simpler solution here.

For example I found this thread: How Can I remove a particular product from an completed order in woocommerce?

foreach ($order->get_items() as $item_id => $item) {
  if ($item_id == 3) {
    wc_delete_order_item($item_id);
  }
}

Here it is explained how to remove an order-item from the order.

We would need a Function that:

  • Opens an order
  • Saves the order_item_id to itself
  • Deletes the order_item
  • adds the order_item again
  • checks if there was a coupon
  • if yes: delete old voucher & add new voucher

Does anyone have any ideas or can help us with this? Thanks a lot!

0 Answers
Related