Method: Shoppe::OrderItem.add_item

Defined in:
app/models/shoppe/order_item.rb

.add_item(ordered_item, quantity = 1) ⇒ Shoppe::OrderItem

This allows you to add a product to the scoped order. For example Order.first.order_items.add_product(…). This will either increase the quantity of the value in the order or create a new item if one does not exist already.

Parameters:

  • ordered_item (Object)

    an object which implements the Shoppe::OrderableItem protocol

  • quantity (Fixnum) (defaults to: 1)

    the number of items to order

Returns:

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/shoppe/order_item.rb', line 48

def self.add_item(ordered_item, quantity = 1)
  raise Errors::UnorderableItem, ordered_item: ordered_item unless ordered_item.orderable?
  transaction do
    if existing = self.where(ordered_item_id: ordered_item.id, ordered_item_type: ordered_item.class.to_s).first
      existing.increase!(quantity)
      existing
    else
      new_item = self.create(ordered_item: ordered_item, quantity: 0)
      new_item.increase!(quantity)
      new_item
    end
  end
end