Handling Checkout form instance
"Checkout instance" is a Javascript object created when Checkout 3 Form is initialised in a page. It contains various functions for interacting with Checkout 3 Form. More information about embedding the form can be found in the article "Embed Checkout Form".
Checkout Instance type
| Property | Description |
|---|---|
refreshForm | Refresh the form – allowing the Checkout Form to load the latest data from the Checkout Backend API - more information about optional features can be found under refreshForm additional options |
unmount | Remove Checkout Form from the page |
beforeSubmitAbort | Function for handling client side validation flow- more information can be found in the article "Client-side validation" |
beforeSubmitContinue | Function for handling client side validations flow – more information can be found in the article "Client-side validation" |
deliveryAddressChangedContinue | Function for handling deliveryAddressChanged callback – more information can be found in the article "Delivery address changed callback" |
clearCustomerToken | Remove customer token from the current page – more information can be found in the article "Customer Token" |
changeLanguage | Change language to desired supported language – more information can be found in the article "Change language" |
purchaseId | A "PurchaseId" connected to the checkout instance. Unique identifier for a purchase. Can be used to confirm the checkout instance belongs to the previously initialized purchase session. |
Accessing the checkout instance
- Storing Checkout instance returned from
window.avardaCheckoutInit - Using Checkout instance attached to
windowobject - Using Checkout instance from callbacks payload
1. Storing Checkout instance returned from window.avardaCheckoutInit
After calling window.avardaCheckoutInit with required payload the function will return checkout instance as it’s return value.
You can store a reference to this CheckoutInstance object while user is on the page where the Checkout Form is displayed to interact with it.
2. Using Checkout instance attached to window object
After calling window.avardaCheckoutInit with required payload the function will attach the CheckoutInstance to the window object allowing you to access the Checkout instance directly by calling window.avardaCheckout.
Example:
3. Using Checkout instance from callbacks payload
All callbacks registered in Checkout Form initialisation will receive CheckoutInstance as last argument. In the following example we can see two different callbacks, in deliveryAddressChangedCallback the registered callback will receive the data payload as a first argument and Checkout instance as a second argument – the instance can be used to handle the callback correctly either by refreshing the form or confirming with the deliveryAddressChangedContinue. In completedPurchaseCallback there is no data payload so only the Checkout instance is received and can be used to remove the Checkout Form from the page.
Example:
Make sure to check each of the callbacks documentation to confirm the data contract.
refreshForm additional options
Anytime you want to call avardaCheckout.refreshForm() you can decide to pass in a reason for calling refreshForm. This is completely optional and should only be used if your specific integration requires it (e.g. using Partner shipping module, using external shipping module, synchronizing multiple windows with the same purchase, etc.).
avardaCheckout.refreshForm({reason: "Default"});
You can use one of the following reasons:
| Reason value | Description |
|---|---|
Default | Default behavior, triggers refreshForm, Checkout Form will call Checkout backend API to get the latest information about the purchase; |
UpdatedUserInfo | In addition to the default behavior, Checkout Form is notified that user information has been updated externally - forcing the Checkout Form to invalidate frontend cache for userInputs fields; |
UpdatedUserInfoEmail | Similar to UpdatedUserInfo but only invalidates user's email address; |
UpdatedUserInfoPhone | Similar to UpdatedUserInfo but only invalidates user's phone; |
UpdatedDeliveryAddress | This reason should be used when delivery address has been changed by a partner by sending the delivery address to Checkout backend API Update delivery address for an ongoing session while you are using a Partner Shipping module Partner Shipping; |
UpdatedItems | This reason should be used when items have been updated by a partner by sending the update items call to Checkout backend API Update Items while you are using a Partner Shipping module Partner Shipping; |
InvalidateCache | In addition to the default behavior, Checkout Form will invalidate all data cached on frontend - useful for synchronizing multiple windows/tabs with the same purchase; |
Default behavior is assumed if no reason for calling refreshForm is provided. This should be correct for the
majority of Checkout integrations. Send the reason for calling refreshForm only if you need one of the above
mentioned non-standard behaviors enabled in your Checkout integration. If you are not sure this is applicable for your
use case, contact our support and we will be happy to assist you setting up the desired behavior.
Removing Checkout3 Form from the page
Do not remove the application by deleting the root element <div> directly. This could lead to memory leaks and incomplete cleanup. Always use the .unmount() function.
When it comes to removing the application from your web page, it’s crucial to follow the correct procedure to ensure proper cleanup and prevent potential issues.
This ensures that the application is properly deleted from memory, the instance is removed from the window, and the associated <div> element is also removed from the DOM.
window.avardaCheckout.unmount();