Skip to content
Sajan Mishra edited this page Oct 17, 2019 · 19 revisions

To use cart methods, checkoutSession is required.

const c = UP_SDK.checkoutSession(params)

Read more about checkout session

Available methods:

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.

Add item to cart:

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.

Remove item from cart:

c.removeFromCart(id, qty)

This takes id as first argument and qty as second argument (opptional). Default value of qty is 1;

Add compound item to the cart:

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:

1. Create a compound item object.

  • 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})

2. Check validity.

ci.isValid() - This checks if the options satisfies the minimum selectables.

2. Add it to cart.

  • Add compound item to cart - c.addToCart(ci.getItemData(), qty)

Cart obj:

1. Get cart

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.

1. Set cart

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.

Clone this wiki locally