-
Notifications
You must be signed in to change notification settings - Fork 0
Cart
To use cart methods, checkoutSession
is required.
const c = UP_SDK.checkoutSession(params)
Read more about checkout session
Methods | Params | Description |
---|---|---|
addToCart |
itemObj, qty | Add item to the cart |
removeFromCart |
-- | Remove item from the cart |
getCart |
-- | Return the cart object |
setCart |
< cart object > | Replace the existing cart obj |
- These methods are NOT async.
c.addToCart(itemObj, qty)
This takes itemObj
as first argument and qty
as second argument (opptional). Default value of qty
is 1;
itemObj
has to be a valid item obj.
c.removeFromCart(id, qty)
This takes id
as first argument and qty
as second argument (opptional). Default value of qty
is 1;
Note: Currently SDK supports validation only for option groups. It's recommended to handle the validation explicitly for n-level.
Items having options and nested options are called compound item.
Adding a compound item to cart involves 2 steps:
- Create instance -
let ci = c.createCompoundItem(itemObj)
- Add options to item -
ci.addToOptions({id, group_id})
- Remove options from item -
ci.removeFromOptions({id, group_id})
ci.isValid()
- This checks if the options satisfies the minimum selectables.
- Add compound item to cart -
c.addToCart(ci.getItemData(), qty)
c.getCart()
This will return cart object with related informations.
i.e.
{
"success": true,
"data": {
"items": [
{
"id": 123,
"item_title": "dummy item",
"quantity": 4,
"item_price": 250
}
],
"total": 250
}
"totalItemsCount": 0
}
totalItemsCount
is the sum of all items along with their quantities.
c.setCart(cartObj)
To restore a saved checkout session, .setCart(cartObj)
can be called with a valid cart object.
NOTE: Cart object - (c.getCart().data
) can be stored in localStorage to re-use it later.