new EcomCart(storeIdopt, storageKeyopt, localStorageopt)
- Source:
 
    Construct a new shopping cart instance object.
    Examples
// Default instance
const ecomCart = new EcomCart()
    // Defining Store ID other than the configured on `$ecomConfig`
const storeId = 2000
const customEcomCart = new EcomCart(storeId)
    Parameters:
| Name | Type | Attributes | Default | Description | 
|---|---|---|---|---|
storeId | 
            
            number | 
                
                    <optional> | 
            
            
                
                
                    $ecomConfig.get('store_id')
                
                 | 
            
            Preset Store ID number | 
storageKey | 
            
            string | null | 
                
                    <optional> | 
            
            
                
                
                    ecomShoppingCart
                
                 | 
            
            Item key to persist cart data | 
localStorage | 
            
            object | 
                
                    <optional> | 
            
            
                
                
                    window.localStorage
                
                 | 
            
            Local Storage interface | 
Members
(static) ecomCart.Constructor :function
- Source:
 - See:
 
    Construct a new shopping cart instance object.
    Type:
- function
 
(static) ecomCart.data :object
- Source:
 
Properties:
| Name | Type | Description | 
|---|---|---|
_id | 
            
            string | Cart object ID | 
items | 
            
            array.<object> | List of cart items | 
subtotal | 
            
            number | Cart subtotal value | 
    Shopping cart data following
E-Com Plus cart object model.
    Type:
- object
 
(static) ecomCart.localStorage :object
- Source:
 
Type:
- object
 
(static) ecomCart.storageKey :string|null
- Source:
 
    Item key to handle persistent JSON EcomCart#data
with localStorage.
    Type:
- string | null
 
(static) ecomCart.storeId :number
- Source:
 
    Respective Store ID number.
    Type:
- number
 
Methods
addItem(newItem, canSaveopt) → {object|null}
- Source:
 
    Push new item to cart data and save.
    Example
ecomCart.addItem({
  _id: '12300000000000000000000f',
  product_id: '123a5432109876543210cdef',
  sku: 's-MP_2B4',
  name: 'Mens Pique Polo Shirt',
  quantity: 4,
  price: 42.9,
  keep_item_price: false
})
    Parameters:
| Name | Type | Attributes | Default | Description | 
|---|---|---|---|---|
newItem | 
            
            object | New cart item object valid for E-Com Plus `cart.items` | ||
canSave | 
            
            boolean | 
                
                    <optional> | 
            
            
                
                
                    true
                
                 | 
            
            Save cart data | 
Returns:
    Returns the saved item object (with `_id`) or null
when new item object is invalid.
- Type
 - object | null
 
addProduct(product, variationIdopt, quantityopt, canSaveopt) → {object|null}
- Source:
 
    Parse product object to item, push to cart and save.
    Example
ecomCart.addProduct({
  _id: '123a5432109876543210cdef',
  sku: 's-MP_2B4',
  commodity_type: 'physical',
  name: 'Mens Pique Polo Shirt',
  slug: 'mens-pique-polo-shirt',
  available: true,
  visible: true,
  price: 42.9,
  price_effective_date: {
    end: '2018-12-01T10:00:00.612Z'
  },
  base_price: 60,
  currency_id: 'BRL',
  currency_symbol: 'R$',
  quantity: 100
})
    Parameters:
| Name | Type | Attributes | Default | Description | 
|---|---|---|---|---|
product | 
            
            object | Product object | ||
variationId | 
            
            string | 
                
                    <optional> | 
            
            
                ID of selected variation if any | |
quantity | 
            
            number | 
                
                    <optional> | 
            
            
                
                
                    1
                
                 | 
            
            Item quantity added | 
canSave | 
            
            boolean | 
                
                    <optional> | 
            
            
                
                
                    true
                
                 | 
            
            Save cart data | 
Returns:
    Returns the saved item object (with `_id`) or null
when new item object is invalid.
- Type
 - object | null
 
clear(canSaveopt) → {self}
- Source:
 
    Remove all items from cart and save.
    Example
ecomCart.clear()
    Parameters:
| Name | Type | Attributes | Default | Description | 
|---|---|---|---|---|
canSave | 
            
            boolean | 
                
                    <optional> | 
            
            
                
                
                    true
                
                 | 
            
            Save empty cart to local storage | 
Returns:
- Type
 - self
 
fixItem(item, canSaveopt) → {object|null}
- Source:
 
    Check and fix (if needed) item quantity, final price and cart subtotal.
    Example
const item = ecomCart.addItem({
  _id: '12300000000000000000000f',
  product_id: '123a5432109876543210cdef',
  sku: 's-MP_2B4',
  name: 'Mens Pique Polo Shirt',
  quantity: 4,
  price: 42.9,
  keep_item_price: false
})
item.customizations = [{
  _id: 'c2300000000000000000000f',
  label: 'Custom name',
  option: {
    text: 'Jonh Doe'
  },
  add_to_price: {
    type: 'fixed',
    addition: 10
  }
}]
item.quantity = 6
ecomCart.fixItemQnt(item)
    Parameters:
| Name | Type | Attributes | Default | Description | 
|---|---|---|---|---|
item | 
            
            string | The cart item object (by reference) | ||
canSave | 
            
            boolean | 
                
                    <optional> | 
            
            
                
                
                    true
                
                 | 
            
            Save cart data | 
Returns:
    Returns the updated item object or null
when item not found.
- Type
 - object | null
 
increaseItemQnt(itemId, quantityopt, canSaveopt) → {object|null}
- Source:
 
    Increase quantity of specific item by ID and save cart.
    Example
ecomCart.increaseItemQnt('12300000000000000000000f', 3)
    Parameters:
| Name | Type | Attributes | Default | Description | 
|---|---|---|---|---|
itemId | 
            
            string | The unique object ID of item | ||
quantity | 
            
            integer | 
                
                    <optional> | 
            
            
                
                
                    1
                
                 | 
            
            Quantity to increase (can be negative) | 
canSave | 
            
            boolean | 
                
                    <optional> | 
            
            
                
                
                    true
                
                 | 
            
            Save cart data | 
Returns:
    Returns the updated item object or null
when item not found.
- Type
 - object | null
 
parseProduct(product, variationIdopt, quantityopt) → {object|null}
- Source:
 
    Parse product object to cart item object.
    Example
const item = ecomCart.parseProduct({
  _id: '123a5432109876543210cdef',
  sku: 's-MP_2B4',
  commodity_type: 'physical',
  name: 'Mens Pique Polo Shirt',
  slug: 'mens-pique-polo-shirt',
  available: true,
  visible: true,
  price: 42.9,
  price_effective_date: {
    end: '2018-12-01T10:00:00.612Z'
  },
  base_price: 60,
  currency_id: 'BRL',
  currency_symbol: 'R$',
  quantity: 100
})
    Parameters:
| Name | Type | Attributes | Default | Description | 
|---|---|---|---|---|
product | 
            
            object | Product object | ||
variationId | 
            
            string | 
                
                    <optional> | 
            
            
                ID of selected variation if any | |
quantity | 
            
            number | 
                
                    <optional> | 
            
            
                
                
                    1
                
                 | 
            
            Item quantity added | 
Returns:
    Returns the parsed item object (with `_id`) or null
when product object is invalid.
- Type
 - object | null
 
removeItem(itemId, canSaveopt) → {object|null}
- Source:
 
    Remove specific item from cart by ID.
    Example
ecomCart.removeItem('12300000000000000000000f')
    Parameters:
| Name | Type | Attributes | Default | Description | 
|---|---|---|---|---|
itemId | 
            
            string | The unique object ID of item | ||
canSave | 
            
            boolean | 
                
                    <optional> | 
            
            
                
                
                    true
                
                 | 
            
            Save cart data | 
Returns:
    Returns the removed item object or null
when item not found.
- Type
 - object | null
 
reset(canSaveopt) → {self}
- Source:
 
    Reset all cart data and create new random ID.
    Example
ecomCart.reset()
    Parameters:
| Name | Type | Attributes | Default | Description | 
|---|---|---|---|---|
canSave | 
            
            boolean | 
                
                    <optional> | 
            
            
                
                
                    true
                
                 | 
            
            Save new cart to local storage | 
Returns:
- Type
 - self
 
save(canFixSubtotalopt) → {self}
- Source:
 
    Save cart object to local storage.
    Example
ecomCart.save()
    Parameters:
| Name | Type | Attributes | Default | Description | 
|---|---|---|---|---|
canFixSubtotal | 
            
            boolean | 
                
                    <optional> | 
            
            
                
                
                    true
                
                 | 
            
            Fix cart subtotal before saving | 
Returns:
- Type
 - self
 
Events
addItem
- Source:
 
Properties:
| Name | Type | Description | 
|---|---|---|
data | 
            
            object | Shopping cart data | 
item | 
            
            object | Item added to cart | 
Type:
- object
 
Example
ecomCart.on('addItem', ({ data, item }) => { console.log(data, item) })
        
            
    
    change
- Source:
 
Properties:
| Name | Type | Description | 
|---|---|---|
data | 
            
            object | Shopping cart data | 
Type:
- object
 
Example
ecomCart.on('change', ({ data }) => { console.log(data.items) })
        
            
    
    clear
- Source:
 
Properties:
| Name | Type | Description | 
|---|---|---|
data | 
            
            object | Shopping cart data | 
Type:
- object
 
Example
ecomCart.on('clear', ({ data }) => { console.log(data.subtotal === 0) })
        
            
    
    fixItem
- Source:
 
Properties:
| Name | Type | Description | 
|---|---|---|
data | 
            
            object | Shopping cart data | 
item | 
            
            object | Cart item changed | 
Type:
- object
 
Example
ecomCart.on('fixItem', ({ data, item }) => { console.log(data, item) })
        
            
    
    increaseItemQnt
- Source:
 
Properties:
| Name | Type | Description | 
|---|---|---|
data | 
            
            object | Shopping cart data | 
item | 
            
            object | Cart item changed | 
Type:
- object
 
Example
ecomCart.on('increaseItemQnt', ({ data, item }) => { console.log(data, item) })
        
            
    
    removeItem
- Source:
 
Properties:
| Name | Type | Description | 
|---|---|---|
data | 
            
            object | Shopping cart data | 
item | 
            
            object | Cart item removed | 
Type:
- object
 
Example
ecomCart.on('removeItem', ({ data, item }) => { console.log(data, item) })
        
            
    
    reset
- Source:
 
Properties:
| Name | Type | Description | 
|---|---|---|
data | 
            
            object | Shopping cart data | 
Type:
- object
 
Example
ecomCart.on('reset', ({ data }) => { console.log(data._id) })
        
            
    
    save
- Source:
 
Properties:
| Name | Type | Description | 
|---|---|---|
data | 
            
            object | Shopping cart data | 
Type:
- object
 
Example
ecomCart.on('save', ({ data }) => { console.log(data.subtotal) })