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

_addToCartMulti

How to Use _addToCartMulti Method#

The _addToCartMulti method adds multiple items to the shopping cart in one batch. This method is useful when you need to update the cart with several products or variants simultaneously. It updates the cart in local storage, emits a change event, and optionally performs additional actions for each item added.

Usage Steps:#

1.
Prepare the Cart Items:
The method requires a single parameter, cartItems, which should be an array of objects. Each object represents a product variant to be added to the cart and must include at least the productId, variantId, and quantity.
2.
Call the Method:
Call the _addToCartMulti method with the prepared array of cart items.
3.
Handle the Response:
The method updates local storage with the new cart data and emits a "cart-changed" event to notify other parts of the application about the cart update.

Example:#

In this example:
The cartItems array contains two items to be added to the cart. Each item includes the productId, variantId, and quantity.
The _addToCartMulti method is called with this array to update the cart in one batch.

Common Use Cases:#

1.
Bulk Adding Items:
Use this method to add multiple items to the cart at once, such as when a user adds several products from a shopping list or a batch action in the application.
2.
Synchronizing Cart Data:
This method helps synchronize cart data with the server in a single API call, reducing the number of requests and improving performance.
3.
Updating UI and State:
After adding items to the cart, the method emits a "cart-changed" event to update the UI and other related components, such as updating the cart icon or displaying a notification.

Notes:#

Local Storage Management: The method updates cartToken and cartData in local storage to ensure that the cart state is consistent across different parts of the application.
Event Emission: The method emits a "cart-changed" event after the cart is updated. This event allows other parts of the application to react to the cart changes, such as refreshing the cart display.
Batch Processing: By handling multiple items in a single API call, the method reduces the number of network requests and improves efficiency, especially when dealing with bulk actions.
This method provides an efficient way to manage multiple cart updates, improving the user experience and performance in e-commerce applications.
Previous
_findBrandsByIds
Next
_addToCart
Built with