Checkout 3.0
Delivery address changed callback

Delivery address changed callback

Optional callback that allows partner to calculate and re-calculate shipping price or to update any other data that are tied to delivery address, zip, country, etc. based on the user inputs in the Checkout3 “Invoice address” or “Delivery address” forms and update the purchase accordingly.

Callback is triggered in multiple cases:

  1. Checkout3 form is refreshed either by calling refreshForm() on the Checkout3 instance or by reloading the page in browser.
  2. “Invoice Address” is added or edited if the user has not provided “Delivery Address”
  3. “Delivery Address” is added, edited or removed by the user.

Checkout3 makes sure the data payload for the callback is correct and relevant for the calculation of the shipping by the partner. Partner needs to process the callback.

Delivery address is different, shipping price needs to be updated

Callback is called with a new payload (or different data from previously stored information), partner is able to use Update Items feature to update the total cost of the purchase and call refreshForm() on the Checkout3 instance to make sure the correct information is displayed in the Checkout3 form.

Delivery address is the same, shipping price doesn’t need to be updated

Callback is called with the same payload as previously, the shipping costs doesn’t have to be updated, partner calls deliveryAddressChangedContinue() to continue with no need to update items and refreshing the form.

Registering a callback

To use this optional callback, partner has to pass an additional flag deliveryAddressChangedCallback to avardaCheckoutInit. Please refer to Showing the checkout form for additional info about Checkout 3 Form initialisation.

Callback Payload

Callback payload is an object with zip, country and source string properties as the first parameter and checkoutInstance as the second parameter.

PropertyData typeDescription
zipstringZIP of the changed address
countrystringISO country code alpha2 format – SE, FI, etc.
sourcestring
“invoice”|”delivery”
Indicates which address has triggered the deliveryAddressChanged callback.

Contents of the first payload object

Below you can see a code example.

<script>
  var deliveryAddressChangedCallback = function({zip, country, source}, checkoutInstance) {
    console.log("Zip: ", zip);
    console.log("Country: ", country);
    console.log("Source: ", source);
 
    ...
    // If Delivery address has changed
    // Handle Update items
    // Refresh form to display the new updated data
    checkoutInstance.refreshForm();
 
    // Delivery address is the same as stored
    // Shipping price is the same as was before
    // Continue
    checkoutInstance.deliveryAddressChangedContinue();
  }
 
  window.avardaCheckoutInit({
    ...
    "deliveryAddressChangedCallback": deliveryAddressChangedCallback,
    ...
  });
</script>