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

_setCart

How to Use _setCart Method#

The _setCart method updates the entire cart with new data. It handles network requests to update the cart on the server, manages a cancel token to prevent redundant requests, and updates local storage with the latest cart information. This method is useful for situations where you need to replace or synchronize the entire cart's data.

Usage Steps:#

1.
Prepare the Cart Data:
The method requires one parameter, cartData, which should be an object containing the updated cart information. This typically includes all the items currently in the cart and any other relevant cart details.
2.
Call the Method:
Call the _setCart method with the prepared cartData object. The method will handle the API request and update the cart.
3.
Handle the Response:
The method updates the cart data in local storage and emits a "cart-changed" event. It also calls _publishInCartItemsChanged to perform any additional actions related to the cart update.

Example:#

In this example:
The cartData object contains the updated cart information, including an array of items and any additional details.
The _setCart method is called with this object to update the cart on the server and in local storage.

Common Use Cases:#

1.
Syncing Cart Data:
Use this method when you need to update the cart with a new set of data, such as after editing the cart or performing a bulk update.
2.
Handling Network Requests:
The method manages a cancel token to prevent redundant or unnecessary requests if multiple updates are triggered in quick succession.
3.
Updating UI and State:
After updating the cart, the method emits a "cart-changed" event to notify other parts of the application about the change. This can be used to refresh the cart display or trigger other UI updates.

Notes:#

Cancel Token Management: The method uses axios.CancelToken to cancel any ongoing requests before starting a new one. This helps prevent issues with overlapping or redundant network requests.
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 updating the cart, allowing other components to react to the cart update. Additionally, it calls _publishInCartItemsChanged for any further actions related to the updated cart data.
This method provides a way to update the entire cart efficiently, handling both network requests and local storage updates, making it suitable for comprehensive cart management in e-commerce applications.
Previous
_addToCart
Next
_clearCart
Built with