# Cart

The cart is stored server-side.
<br>
The JavaScript API sends requests to the server and returns
[JavaScript promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise).

## Usage

```javascript
import { mockServer, CartApi } from 'geta-checkout';

const cartApi = new CartApi(mockServer.cartServices);

cartApi.get()
    .then(cart => console.log('The server returned a cart!'))
    .then(() => cartApi.add('apple'))
    .then(() => cartApi.add('mango'))
    .then(() => cartApi.add('lemon'))
    .then(() => console.log('The cart contains three fruits.'))
    .then(() => cartApi.remove('mango'))
    .then(cart => console.log(`The cart contains ${cart.totalQuantity} fruits.`));
```

> **See [how to configure services](../readme.md)**

## Properties

| Name                    | Type                        | Description                                                   |
|-------------------------|-----------------------------|---------------------------------------------------------------|
| appliedCouponCodes      | Array                       | An array of coupon codes. Example:<br>`[{ name: 'Your discount', code: '1234', discount: 100 }]` |
| items                   | Array                       | An array of [CartItem](../cart-item/readme.md)s               |
| totalQuantity           | Number                      | The total quantity of all [CartItem](../cart-item/readme.md)s |
| taxPrice                | [Price](../price/readme.md) | |
| totalPrice              | [Price](../price/readme.md) | |
| totalPriceWithoutTax    | [Price](../price/readme.md) | |
| totalDiscount           | [Price](../price/readme.md) | &nbsp; |

## Methods

### `add(itemId, quantity=1)`

→ `cart` or `Error`

Adds an item to cart.
<br>
An item could be a *product variation* or a *package*.
<br>
Examples of product variations could be "small shirt" or "red pants".
<br>
A package consists of several product variations, for example an "outfit".

```javascript
cartApi.add('id', 3)
    .then(console.log)
    .catch(console.error);
```

### `remove(itemId, quantity=null)`

→ `cart` or `Error`

Removes an item from cart.
<br>
If no quantity is passed, the entire item will be removed.

```javascript
cartApi.remove('id', 1)
    .then(console.log)
    .catch(console.error);
```

### `empty()`

→ `undefined` or `Error`

Empties the entire cart.

```javascript
cartApi.empty()
    .then(console.log)
    .catch(console.error);
```

### `get()`

→ `cart` or `Error`

Retrieves the cart.

```javascript
cartApi.get()
    .then(console.log)
    .catch(console.error);
```

### `contains(itemId)`

→ `Boolean` or `Error`

Checks if an item has been added to cart.

```javascript
cart.contains('id')
    .then(console.log)
    .catch(console.error);
```

### `applyCouponCode(code)`

→ `cart` or `Error`

Applies a coupon code.

```javascript
cartApi.applyCouponCode('my-coupon-code')
    .then(console.log)
    .catch(console.error);
```

## Tests

[Run unit tests](../../test/index.html?grep=Cart)
