ec_cart v0.1.5 EcCart.Cart

Module to handle EcCart structures.

Summary

Functions

Add adjustment to the adjustment list

Add a new item into the %EcCart.Cart{} structure, if the item already exists on the structure, this function will update the quantity

Creates an empty %EcCart.Cart{} structure, this strucure has by default and empty list of items and and empty list of adjustments

Remove adjustment from adjustment list

Calculate the sum of the result of multiply the price of each item and its quantity

Calculates the total of the cart that include: subtotal + adjustments

Functions

add_adjustment(ec_cart, ec_cart_adjustment)

Add adjustment to the adjustment list.

## Examples

iex> ec_cart = EcCart.Cart.new
  iex> adj = EcCart.Adjustment.new("shipping","Shipping",
  ...> fn(x) ->
  ...> sb = EcCart.Cart.subtotal(x)
  ...>  case sb do
  ...>    sb when sb > 25 -> 0
  ...>    _-> 10
  ...>   end
  iex> end)
  iex> ec_cart = EcCart.Cart.add_adjustment(ec_cart,adj)
  iex> length ec_cart.adjustments
  1
add_item(ec_cart, ec_cart_item)

Add a new item into the %EcCart.Cart{} structure, if the item already exists on the structure, this function will update the quantity.

## Examples

iex> ec_cart = EcCart.Cart.new
  iex> EcCart.Cart.add_item(ec_cart,%EcCart.Item{ ec_sku: "SU04", ec_qty: 10, ec_price: 3 })
  %EcCart.Cart{adjustments: [],items: [%EcCart.Item{attr: %{}, ec_price: 3, ec_qty: 10, ec_sku: "SU04"}]}
new()

Creates an empty %EcCart.Cart{} structure, this strucure has by default and empty list of items and and empty list of adjustments

## Examples

iex> EcCart.Cart.new
  %EcCart.Cart{adjustments: [], items: []}
remove_adjustment(ec_cart, adj)

Remove adjustment from adjustment list.

## Examples

iex> eccart = EcCart.Cart.new iex> adj = EcCart.Adjustment.new(“shipping”,”Shipping”, …> fn(x) -> …> sb = EcCart.Cart.subtotal(x) …> case sb do …> sb when sb > 25 -> 0 …> -> 10 …> end iex> end) iex> ec_cart = EcCart.Cart.add_adjustment(ec_cart, adj) iex> length ec_cart.adjustments 1 iex> ec_cart = EcCart.Cart.remove_adjustment(ec_cart, “shipping”) iex> length ec_cart.adjustments 0

subtotal(cart)

Calculate the sum of the result of multiply the price of each item and its quantity

## Examples

iex> ec_cart = EcCart.Cart.new
  iex> ec_cart = EcCart.Cart.add_item(ec_cart,%EcCart.Item{ ec_sku: "SU04", ec_qty: 10, ec_price: 2 })
  iex> EcCart.Cart.subtotal(ec_cart)
  20
total(ec_cart)

Calculates the total of the cart that include: subtotal + adjustments

## Examples

iex> ec_cart = EcCart.Cart.new
iex> ec_cart = EcCart.Cart.add_item(ec_cart,%EcCart.Item{ ec_sku: "SU04", ec_qty: 5, ec_price: 3 })
iex> adj = EcCart.Adjustment.new("shipping","Shipping", 
...> fn(x) -> 
...> sb = EcCart.Cart.subtotal(x)
...>case sb do
...>  sb when sb > 25 -> 0
...>  _-> 10
...>end
...>end)
iex> ec_cart = EcCart.Cart.add_adjustment(ec_cart, adj)
iex> EcCart.Cart.total(ec_cart)
25