HomeWiki
HomeWiki
  1. Checkout
  • Back to home
  • 1. Themes
  • Layout
    • New Layout
    • Legacy Layout
  • Components
    • Announcement
    • Banner Carousel
    • Banner With Products Carousel
    • Blog Category List
    • Blog List
    • Brand List
    • Brands Carousel
    • Breadcrumb
    • Call To Action
    • Cart
    • Categories List
    • Change Password
    • Checkout
    • Cookie Manager
    • Filter list
    • Footer
    • Forgot Password
    • Form
    • Hero Carousel
    • Icon Block
    • Invitation
    • Last Visited Products
    • Layout
    • Login
    • Map
    • Nav Bar
    • Offer
    • Product Attachments
    • Product Attributes
    • Product Documentation
    • Product Expected
    • Product Modal
    • Products Block
    • Products Carousel
    • Product Single
    • Profile
    • Quote
    • Register
    • Related Products
    • Search
    • Stores
    • Subscribe Newsletter
    • Text with Image
    • Top Bar
    • Video
  • Reusables
    • Getting Started
  • Assets
    • Getting Started
  • SDK
    • Products
      • _findProductsByCategory
      • _findProductsByIds
      • _findProductsByTitle
      • _findProductsByFilter
      • _findProductsByCriteria
      • _findProductsAndCalculate
      • _findProductsThenCalculate
      • _getProductAttributeSet
      • _setLastVisited
    • Categories
      • _findCategoryTreeById
      • _findCategoriesByIds
      • _findCategoryByAlias
      • _findCategoryTreeByAlias
      • _getCategoryContent
    • Collections
      • _getCollectionContent
      • _findCollectionsByIds
      • _findCollectionsByIdsThenCalculate
    • Brands
      • _getBrandContent
      • _findBrandsByIds
    • Cart
      • _addToCartMulti
      • _addToCart
      • _setCart
      • _clearCart
      • _setCartListener
      • _removeFromCart
      • _calculateCart
    • Checkout
      • _startCheckout
      • _updateCheckout
      • _completeCheckout
    • Shopping Lists
      • _getShoppingLists
      • _updateShoppingList
      • _createShoppingList
      • _deleteShoppingList
      • _getShoppingListByAlias
    • Navigation
      • _getFooterMenu
      • _getHeaderMenu
    • Users
      • _getUserById
    • Utils
      • _calculateCurrency
      • _getCurrencySymbol
      • _getCulture
      • _subscribeToNewsletter
      • _findUnitsByIds
  • Vs Code
    • Getting Started
  1. Checkout

_completeCheckout

Example Usage of _completeCheckout Method#

The _completeCheckout method is designed to finalize the checkout process. It sends a request to the server to complete the checkout, and upon successful completion, it clears relevant local storage items related to the cart and checkout. This method is typically called when the user has finished the checkout process and is ready to finalize their purchase.

Scenario#

A user is finalizing their purchase after reviewing their cart and entering payment details. The _completeCheckout method will complete the checkout, clear local storage related to the cart, and trigger necessary updates in the application.

Example Code#

Explanation#

1.
Define Callback Functions:
handleCompleteSuccess: This function is called when the checkout is successfully completed. It logs the response data and shows an alert to indicate successful completion. You might also redirect the user to a confirmation or thank-you page.
handleCompleteError: This function is called if there is an error during the checkout completion. It logs the error status and data, and displays an alert with an error message.
2.
Call the _completeCheckout Method:
The _completeCheckout method is invoked with the handleCompleteSuccess and handleCompleteError callback functions. This triggers the completion of the checkout process.

Detailed Flow#

1.
Send Completion Request:
The method sends a POST request to the /api/checkout/complete/{checkoutToken} endpoint using the checkoutToken from localStorage. This endpoint finalizes the checkout process on the server.
2.
Handle Response:
On Success:
The checkoutToken, cartToken, and cartData are removed from localStorage, clearing the cart and checkout data.
The cart-changed event is emitted with a null value, indicating that the cart is now empty.
The complete-checkout event is emitted with the response data.
The handleCompleteSuccess callback function is invoked with the response data, allowing you to update the UI or perform additional actions such as redirecting the user to a confirmation page.
On Error:
The handleCompleteError callback function is invoked with the error status and data, allowing you to handle the error appropriately, such as by showing an error message to the user.

Notes#

Token Management: Ensure that localStorage.checkoutToken is correctly managed and valid at the time of the checkout completion request.
Local Storage Cleanup: The method clears relevant local storage items to ensure that the cart and checkout data are removed after completion.
Event Handling: The cart-changed and complete-checkout events can be used to update the application state and user interface accordingly.
By following this example, you can effectively use the _completeCheckout method to finalize the checkout process, clear related data, and handle both successful and erroneous scenarios in a user-friendly manner.
Previous
_updateCheckout
Next
_getShoppingLists
Built with