Cart

The cart is stored server-side.
The JavaScript API sends requests to the server and returns JavaScript promises.

Usage

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

Properties

Name Type Description
appliedCouponCodes Array An array of coupon codes. Example:
[{ name: 'Your discount', code: '1234', discount: 100 }]
items Array An array of CartItems
totalQuantity Number The total quantity of all CartItems
taxPrice Price
totalPrice Price
totalPriceWithoutTax Price
totalDiscount Price  

Methods

add(itemId, quantity=1)

→ cart or Error

Adds an item to cart.
An item could be a product variation or a package.
Examples of product variations could be "small shirt" or "red pants".
A package consists of several product variations, for example an "outfit".

cartApi.add('id', 3)
    .then(console.log)
    .catch(console.error);

remove(itemId, quantity=null)

→ cart or Error

Removes an item from cart.
If no quantity is passed, the entire item will be removed.

cartApi.remove('id', 1)
    .then(console.log)
    .catch(console.error);

empty()

→ undefined or Error

Empties the entire cart.

cartApi.empty()
    .then(console.log)
    .catch(console.error);

get()

→ cart or Error

Retrieves the cart.

cartApi.get()
    .then(console.log)
    .catch(console.error);

contains(itemId)

→ Boolean or Error

Checks if an item has been added to cart.

cart.contains('id')
    .then(console.log)
    .catch(console.error);

applyCouponCode(code)

→ cart or Error

Applies a coupon code.

cartApi.applyCouponCode('my-coupon-code')
    .then(console.log)
    .catch(console.error);

Tests

Run unit tests