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'] &&
!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']->order_number);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
if (!empty($order->commerce_customer_billing)) {
if ($new_country !== $order_wrapper->commerce_customer_billing->commerce_customer_address->country->value()) {
$order_wrapper->commerce_customer_billing->commerce_customer_address->country = $new_country;
$order_wrapper->commerce_customer_billing->save();
$order_wrapper->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