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 |
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
window
object - 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.
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();