2

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

3 Answers 3

1

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.

0

Hmm, it's hard to say exactly what's going on, because saving the order actually does result in a call to commerce_order_calculate_total() automatically. Since that isn't achieving your desired outcome, I wonder what else may be going wrong.

Is it possible that even though you're saving the order you didn't actually update the related customer profile and save that first before triggering a recalculation of the order total? The order itself doesn't have the address, just a reference to the customer profile that contains the address, so you would need to update the customer profile first in order for the new value to be taken into account during price calculation.

2
  • I think the issue I was running into was there was a cached version of the order in the form and so it was getting the old cached total, not the new total. Re-running commerce_order_calculate_total fixed it (for me).
    – Felix Eve
    Oct 26, 2017 at 3:14
  • Thanks for the quick replies. I've edited the question to show where I am so far. My debugged is telling me that the order total structure is not changing, the VAT line is not being added as a component of the total. Oct 26, 2017 at 9:27
0

It turns out that all you really need to do is...

Update the shipping address, not the billing address. The above code works if you update the shipping address, call commerce_cart_order_refresh(), iterate all the line items and save them to force a recalculation of sale price, then call commerce_order_calculate_total()

Thanks to both that replied but I think I'll give the real answer to Felix as he correctly pointed me at the commerce_order_calculate_total() function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.