5 attr_reader :items, :shipping_cost, :tax, :total
7 # Initializes the shopping cart
12 # Empties or initializes the cart
28 # Returns the total price of our cart
32 @total += (item.quantity * item.unit_price)
37 # Defined here because in order we have a line_items_total
38 # That number is the total of items - shipping costs.
43 # Adds a product to our shopping cart
44 def add_product(product, quantity=1)
45 item = @items.find { |i| i.product_id == product.id }
47 item.quantity += quantity
48 # Always set price, as it might have changed...
49 item.price = product.price
51 item = OrderLineItem.for_product(product)
52 item.quantity = quantity
57 # Removes all quantities of product from our cart
58 def remove_product(product, quantity=nil)
59 item = @items.find { |i| i.product_id == product.id }
61 quantity = item.quantity
64 if item.quantity > quantity then
65 item.quantity -= quantity
72 # Checks inventory of products, and removes them if
73 # they're out of stock.
75 # Returns an array of items that have been removed.
80 # Find the item in the db, because oli.item
82 db_item = Item[oli.item_id]
84 if oli.quantity > db_item.quantity
85 removed_items << oli.name.clone