How can I programmatically recalculate an order total after changing the billing country I have various international tax rules in place, specifically for EU VAT at different rates. This is all working as expected.

On the checkout form, when you enter a non-EU address, like Australia, when you go to the next page in the checkout process, the UK VAT line has been totally removed from the order total, exactly as I would expect.

My client wants to update the order summary on the billing page by AJAX to avoid confusion. Australian customers still see the UK VAT rate before they submit their address.

I've got the AJAX update working fine, and can load the order, update the billing country, and save the order. When I then refresh the page the address remains as I changed it, but the order total still reflects the old country.

I have traced through the code to see how the submit handler deals with this and I can't see what I need to do.

Is there a way I can recalculate the order total in code, either in form_alter or the AJAX callback, so that the order object reflects the correct price for the billing country already set?

So, in short, if I change the billing address for an order, in code, how do I tell that order to recalculate it's totals?

I hope that makes sense and thanks for any help you can give.

This is what I'm trying, in hook_form_alter:

  if ($form_state['order'] &amp;&amp;
    !empty($form_state['input']['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['country'])) {

    $new_country = $form_state['input']['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['country'];
    $order = commerce_order_load($form_state['order']-&gt;order_number);
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    if (!empty($order-&gt;commerce_customer_billing)) {
      if ($new_country !== $order_wrapper-&gt;commerce_customer_billing-&gt;commerce_customer_address-&gt;country-&gt;value()) {

        $order_wrapper-&gt;commerce_customer_billing-&gt;commerce_customer_address-&gt;country = $new_country;
        $order_wrapper-&gt;commerce_customer_billing-&gt;save();
        $order_wrapper-&gt;save();

        commerce_order_calculate_total($order);
        commerce_cart_order_refresh($order);
      }
    }
  }


Stepping through each call watching the commerce_order_total and the number of components never changes, no the VAT line never changes and the price never changes. When I press "Continue to next step" the VAT line changes (is either added or removed appropriately).

Very frustrating. I've looked at the submit call and tried to trace through it but can't find where the total is being changed. I'm guessing it's in rules somewhere. If it makes a difference we're using commerce_eu_vat
I've recently run into the exact same problem as you after changing the shipping address in an ajax call.

commerce_order_calculate_total() was the function that got everything working as expected for me.

You may or may not also need to use commerce_cart_order_refresh().

First try without but if you are still not seeing it updated then try both.

If possible run these functions directly before your actually sending the order total value to be rendered.