7 attr_reader :items, :shipping_cost, :tax, :total
9 # Initializes the shopping cart
14 # Empties or initializes the cart
30 # Returns the total price of our cart
34 @total += (item.quantity * item.unit_price)
39 # Defined here because in order we have a line_items_total
40 # That number is the total of items - shipping costs.
45 # Adds a product to our shopping cart
46 def add_product(product, quantity=1)
47 item = @items.find { |i| i.product_id == product.id }
49 item.quantity += quantity
50 # Always set price, as it might have changed...
51 item.price = product.price
53 item = OrderLineItem.for_product(product)
54 item.quantity = quantity
59 # Removes all quantities of product from our cart
60 def remove_product(product, quantity=nil)
61 item = @items.find { |i| i.product_id == product.id }
63 quantity = item.quantity
66 if item.quantity > quantity then
67 item.quantity -= quantity
74 # Checks inventory of products, and removes them if
75 # they're out of stock.
77 # Returns an array of items that have been removed.
82 # Find the item in the db, because oli.item
84 db_item = Item[oli.item_id]
86 if oli.quantity > db_item.quantity
87 removed_items << oli.name.clone