Home
Wiki
Home
Wiki
  1. Cart
  • Back to home
  • 1. Themes
  • Vs Code
    • Getting Started
  • Kitchenware
    • 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
  • Noir
    • 0. Introduction
    • 6. FAQ
    • 1. Structure
      • _Overview
      • LayoutA.liquid
      • ComponentsList.liquid
      • Metas.liquid
      • CssVariables.liquid
      • Json.liquid
      • GoogleTagManager.liquid
      • StagingButton.liquid
    • 2. Components
      • _Overview
      • Announcement
      • BannerCarousel
      • BlogCategoryList
      • BlogHighlights
      • BlogList
      • BrandList
      • Breadcrumb
      • Cart
      • CategoriesList
      • ChangePassword
      • Checkout
      • CookieManager
      • CustomTable
      • Faq
      • FilterList
      • Footer
      • ForgotPassword
      • Form
      • IconBlock
      • Invitation
      • LastVisitedProducts
      • Login
      • Map
      • NavBar
      • Offer
      • ProductAttachments
      • ProductAttributes
      • ProductComparison
      • ProductDocumentation
      • ProductMixList
      • ProductsBlock
      • ProductsCarousel
      • ProductSingle
      • Profile
      • Quote
      • Register
      • RelatedProducts
      • SingleBlog
      • Stores
      • TextWithImage
      • ThankYouPage
      • TopBar
      • Wishlist
    • 3. Reusables
      • _Overview
      • Addresses
      • AddressForm
      • AnnouncementModal
      • BackToTop
      • BillingRetail
      • BlogPostingStructuredData
      • BrandListStructuredData
      • CollectionStructuredData
      • Company
      • General
      • GroupChildrenTable
      • Login
      • LoginModal
      • MonthlyTransactions
      • Offers
      • Orders
      • Payment
      • ProductAttachments
      • ProductAttributes
      • ProductAvailabilityAction
      • ProductComparisonButton
      • ProductComparisonFloatingButton
      • ProductGridItem
      • ProductGridItemClassic
      • ProductListItem
      • ProductModal
      • ProductStructuredData
      • ProfileInfo
      • PromptModal
      • Quotes
      • Register
      • Services
      • Shipping
      • ShoppingLists
      • ShoppingListsButton
      • ShoppingListsNavbar
      • StoresStructuredData
      • Toast
      • Transactions
      • Users
      • VariantContent
      • VariantContentClassic
      • WishlistButton
    • 4. Assets
      • Fonts
      • Images
      • Templates
      • Javascript
        • _Overview
        • theme.js
      • Css - Scss
        • _Overview
        • ThemeClasses
    • 5. SDK
      • _Overview
      • LiquidGlobals
      • ServicesSDK
  1. Cart

_addToCart

How to Use _addToCart Method#

The _addToCart method adds a specified product variant to the user's shopping cart. It manages the cart token and updates local storage with the latest cart data. It is fire-and-forget: it returns nothing, so you cannot await it. When you need the result, use _addToCartAsync, which takes the same parameters and returns a promise.

Usage Steps:#

1.
Provide Product Details:
The method takes four parameters:
productId: The unique identifier of the product you want to add.
variantId: The unique identifier of the product variant.
quantity: The quantity of the product variant to add.
productVariant (optional): The variant object. Its price and initialPrice are sent with the Google Analytics addToCart event.
2.
Call the Method:
Call _addToCart with the parameters when you don't need to wait for the result, or await this._addToCartAsync(...) when you do.
3.
Handle the Response:
_addToCart reports the result only through the "cart-changed" event.
_addToCartAsync resolves with the updated cart. It never rejects: if the request fails, it resolves with the error response instead of the cart.

Example:#

Common Use Cases:#

1.
Adding Items to Cart:
This method is used to add products to the shopping cart from various parts of your application, such as product detail pages, product listings, or quick view modals.
2.
Managing Cart Token:
For authenticated users without a cart token, the method requests the user's cart token. _addToCart does not wait for that request before adding the item, so prefer _addToCartAsync when the add may be the user's first cart action.
3.
Updating Cart State and UI:
The method updates the cart in local storage and emits a "cart-changed" event, which can trigger UI updates or other actions related to cart changes, like updating the cart icon or displaying a notification.

Notes:#

Return Value: _addToCart returns nothing. _addToCartAsync returns a promise that resolves with the updated cart, or, if something fails, with the HTTP error response (undefined when the failure was not an HTTP error).
Error Handling: _addToCartAsync catches request errors and resolves with the error response, so check the result instead of wrapping the call in try/catch.
Local Storage Management: The method manages local storage to ensure the cart state is maintained across different parts of the application and between sessions.
Event Emission: After updating the cart, the method emits a "cart-changed" event, allowing other parts of the application to react to the cart update.
This method provides a robust and efficient way to add items to the shopping cart, handle authentication, and manage cart state in an e-commerce application.
Modified at 2026-10-07 12:01:58
Previous
_addToCartMulti
Next
_setCart
Built with