View Source ExCart.Cart (ex_cart v1.1.0)

Module to handle ExCart structures.

Summary

Functions

Add adjustment to the adjustment list.

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

Clear the Cart.

Clear adjustments from the Cart.

Clear items from the Cart.

Get the Adjustment.

Get the Adjustment Result.

Creates an empty %ExCart.Cart{} structure, this structure 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

Link to this function

add_adjustment(cart, adjustment)

View Source

Add adjustment to the adjustment list.

## Examples

  iex> ex_cart = ExCart.Cart.new
  iex> adj = ExCart.Adjustment.new("shipping","Shipping",
  ...> fn(x) ->
  ...> sb = ExCart.Cart.subtotal(x)
  ...>  case sb do
  ...>    sb when sb > 25 -> 0
  ...>    _-> 10
  ...>   end
  iex> end)
  iex> ex_cart = ExCart.Cart.add_adjustment(ex_cart,adj)
  iex> length ex_cart.adjustments
  1
Link to this function

add_item(ex_cart, cart_item)

View Source

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

## Examples

  iex> ex_cart = ExCart.Cart.new
  iex> ex_cart = ExCart.Cart.add_item(ex_cart,%ExCart.Item{ sku: "SU04", qty: 1, price: 3 })
  iex> ex_cart = ExCart.Cart.add_item(ex_cart,%ExCart.Item{ sku: "SU04", qty: 1, price: 3 })
  %ExCart.Cart{adjustments: [],items: [%ExCart.Item{attr: %{}, price: 3, qty: 2, sku: "SU04"}]}
  iex> ExCart.Cart.subtotal(ex_cart)
  6

Clear the Cart.

Clear adjustments from the Cart.

Clear items from the Cart.

Link to this function

get_adjustment(cart, adjustment)

View Source

Get the Adjustment.

Link to this function

get_adjustment_result(cart, adjustment)

View Source

Get the Adjustment Result.

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

## Examples

  iex> ExCart.Cart.new
  %ExCart.Cart{adjustments: [], items: []}
Link to this function

remove_adjustment(cart, adj)

View Source

Remove adjustment from adjustment list.

## Examples

iex> excart = ExCart.Cart.new iex> adj = ExCart.Adjustment.new("shipping","Shipping", ...> fn(x) -> ...> sb = ExCart.Cart.subtotal(x) ...> case sb do ...> sb when sb > 25 -> 0 ...> -> 10 ...> end iex> end) iex> ex_cart = ExCart.Cart.add_adjustment(ex_cart, adj) iex> length ex_cart.adjustments 1 iex> ex_cart = ExCart.Cart.remove_adjustment(ex_cart, "shipping") iex> length ex_cart.adjustments 0

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

## Examples

  iex> ex_cart = ExCart.Cart.new
  iex> ex_cart = ExCart.Cart.add_item(ex_cart,%ExCart.Item{ sku: "SU04", qty: 10, price: 2 })
  iex> ExCart.Cart.subtotal(ex_cart)
  20

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

## Examples

iex> ex_cart = ExCart.Cart.new
iex> ex_cart = ExCart.Cart.add_item(ex_cart,%ExCart.Item{ sku: "SU04", qty: 5, price: 3 })
iex> adj = ExCart.Adjustment.new("shipping","Shipping",
...> fn(x) ->
...> sb = ExCart.Cart.subtotal(x)
...>case sb do
...>  sb when sb > 25 -> 0
...>  _-> 10
...>end
...>end)
iex> ex_cart = ExCart.Cart.add_adjustment(ex_cart, adj)
iex> ExCart.Cart.total(ex_cart)
25