HomeWiki
HomeWiki
  1. Cart
  • 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. Cart

_clearCart

How to Use _clearCart Method#

The _clearCart method removes all items from the user's shopping cart. It handles the API request to clear the cart, updates the local storage to reflect the empty cart, and emits an event to notify other parts of the application about the cart change.

Usage Steps:#

1.
Call the Method:
Invoke the _clearCart method to initiate the process of clearing the cart.
2.
Handle the Response:
The method updates local storage with the latest (empty) cart data and emits a "cart-changed" event to update the UI and other related components.

Example:#

In this example:
Calling the _clearCart method triggers an API request to clear the cart and updates local storage with the new cart state.

Common Use Cases:#

1.
Emptying the Cart:
Use this method when you need to clear all items from the cart, such as when a user performs a checkout or wants to start over with a fresh cart.
2.
Handling Cart State:
The method ensures that local storage and any cart-related UI components are updated to reflect that the cart is now empty.
3.
Resetting Cart Data:
This method is useful for scenarios where you need to reset the cart data completely, such as after user logout or a major update to the cart functionality.

Notes:#

Local Storage Management: The method updates the cartToken and cartData in local storage to reflect the empty cart state. This ensures that the cart state is consistent across different parts of the application.
Event Emission: After clearing the cart, the method emits a "cart-changed" event to notify other components about the cart update. This can be used to refresh the cart display or trigger other UI updates related to the cart.
This method provides a straightforward way to clear the shopping cart and manage the cart state efficiently, making it suitable for various scenarios where you need to remove all items from the cart.
Previous
_setCart
Next
_setCartListener
Built with