wordpresswoocommerce

How to recalculate order taxes and total programmatically in woocommerce


I use woocommerce from Admin only and add orders manually from Admin panel only.

Everything works fine except tax calculation. After adding products, I must click Recalculate to be able to apply relevant taxes. If I forget, invoice does not include taxes.

I am wondering if there's a way to apply all the steps performed by Recalculate button during order update. I have custom meta boxes and many order meta that I save programmatically on order update.

Let me know if the question isn't clear, this is my first post here. Thanks for your help.


Solution

  • This might help a bit into some direction:

            $this->order = new WC_Order($order_id); 
    
            // Order back to pending
            $this->order->update_status('pending', '<%Your message%>', TRUE);
    
            // Recalculate the order
            $total = $this->order->calculate_totals();
    
            // Save it, although the calculate also does it. 
            $this->order->save();
    

    I am not really sure about it, but it might get you going (if still needed)