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

_setCartListener

How to Use _setCartListener Method#

The _setCartListener method allows you to register a callback function that will be executed whenever the shopping cart changes. This method listens for the "cart-changed" event and invokes the provided callback with the event data.

Usage Steps:#

1.
Define a Callback Function:
Create a function that will handle the cart change event. This function should accept one parameter, which will be the event data.
2.
Register the Callback:
Call the _setCartListener method, passing your callback function as an argument.
3.
Handle Cart Changes:
Implement the logic in your callback function to respond to cart changes, such as updating the UI or performing other actions based on the new cart data.

Example:#

In this example:
The handleCartChange function is defined to handle the cart change event. It logs the updated cart data and could also update UI components.
The _setCartListener method is called with handleCartChange to ensure that this function is executed whenever the cart changes.

Common Use Cases:#

1.
Updating UI on Cart Changes:
Use this method to update UI components that display cart information, such as a cart icon with the number of items or a cart summary panel.
2.
Performing Actions on Cart Changes:
This method is useful for executing actions that depend on the cart's state, such as recalculating totals, applying discounts, or triggering notifications.
3.
Synchronizing Cart State:
Registering a listener ensures that your application remains in sync with the current cart state, reflecting any changes made by different parts of the application.

Notes:#

Event Data: The callback function receives the event data, which typically includes the updated cart information. Use this data to update the UI or perform other actions based on the current state of the cart.
Event Emission: The "cart-changed" event is emitted by methods that modify the cart, such as _addToCart, _clearCart, or _setCart. The listener registered with _setCartListener will be triggered whenever one of these methods updates the cart.
This method provides a way to react to changes in the shopping cart, enabling you to keep your application in sync with the latest cart state and perform necessary updates or actions efficiently.
Previous
_clearCart
Next
_removeFromCart
Built with