_addToCart
How to Use _addToCart
Method
_addToCart
method adds a specified product variant to the user's shopping cart. It handles authentication, manages the cart token, and updates local storage with the latest cart data. This method uses async/await syntax for cleaner and more manageable asynchronous code.Usage Steps:
1.
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.2.
_addToCart
method with the required parameters. The method returns a promise that resolves with the response data or an error.3.
async/await
or .then()
/.catch()
to handle the response or error from the method.Example:
Common Use Cases:
1.
2.
3.
"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:
"cart-changed"
event, allowing other parts of the application to react to the cart update.