# EcomCart

#### new EcomCart(storeIdopt, storageKeyopt, localStorageopt)

Source:
  - constructor.js, line 35

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:
  - constructor.js, line 44
See:
  - EcomCart

Construct a new shopping cart instance object.

##### Type:

- function

#### (static) ecomCart.data :object

Source:
  - constructor.js, line 77

##### 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:
  - constructor.js, line 66

Storage interface.

##### Type:

- object

#### (static) ecomCart.storageKey :string|null

Source:
  - constructor.js, line 59

Item key to handle persistent JSON EcomCart#data with localStorage.

##### Type:

- string | null

#### (static) ecomCart.storeId :number

Source:
  - constructor.js, line 51

Respective Store ID number.

##### Type:

- number

### Methods

#### addItem(newItem, canSaveopt) → {object|null}

Source:
  - methods/add-item.js, line 5

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:
  - methods/add-product.js, line 3

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:
  - methods/clear.js, line 1

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

#### increaseItemQnt(itemId, quantityopt, canSaveopt) → {object|null}

Source:
  - methods/increase-item-qnt.js, line 29

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

#### removeItem(itemId, canSaveopt) → {object|null}

Source:
  - methods/remove-item.js, line 3

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:
  - methods/reset.js, line 3

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:
  - methods/save.js, line 3

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:
  - methods/add-item.js, line 64

##### 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:
  - constructor.js, line 94

##### Properties:

Name | Type | Description
--- | --- | ---
`data` | object | Shopping cart data

##### Type:

- object

##### Example

    ecomCart.on('change', ({ data }) => { console.log(data.items) })

#### clear

Source:
  - methods/clear.js, line 24

##### Properties:

Name | Type | Description
--- | --- | ---
`data` | object | Shopping cart data

##### Type:

- object

##### Example

    ecomCart.on('clear', ({ data }) => { console.log(data.subtotal === 0) })

#### increaseItemQnt

Source:
  - methods/increase-item-qnt.js, line 14

##### 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:
  - methods/remove-item.js, line 27

##### 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:
  - methods/reset.js, line 25

##### Properties:

Name | Type | Description
--- | --- | ---
`data` | object | Shopping cart data

##### Type:

- object

##### Example

    ecomCart.on('reset', ({ data }) => { console.log(data._id) })

#### save

Source:
  - methods/save.js, line 28

##### Properties:

Name | Type | Description
--- | --- | ---
`data` | object | Shopping cart data

##### Type:

- object

##### Example

    ecomCart.on('save', ({ data }) => { console.log(data.subtotal) })