Class: Spree::OrderContents

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/order_contents.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order) ⇒ OrderContents

Returns a new instance of OrderContents.



5
6
7
# File 'app/models/spree/order_contents.rb', line 5

def initialize(order)
  @order = order
end

Instance Attribute Details

#currencyObject

Returns the value of attribute currency.



3
4
5
# File 'app/models/spree/order_contents.rb', line 3

def currency
  @currency
end

#orderObject

Returns the value of attribute order.



3
4
5
# File 'app/models/spree/order_contents.rb', line 3

def order
  @order
end

Instance Method Details

#add(variant, quantity = 1, currency = nil, shipment = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'app/models/spree/order_contents.rb', line 9

def add(variant, quantity = 1, currency = nil, shipment = nil)
  line_item = add_to_line_item(variant, quantity, currency, shipment)
  reload_totals
  shipment.present? ? shipment.update_amounts : order.ensure_updated_shipments
  PromotionHandler::Cart.new(order, line_item).activate
  ItemAdjustments.new(line_item).update
  reload_totals
  line_item
end

#remove(variant, quantity = 1, shipment = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'app/models/spree/order_contents.rb', line 19

def remove(variant, quantity = 1, shipment = nil)
  line_item = remove_from_line_item(variant, quantity, shipment)
  reload_totals
  shipment.present? ? shipment.update_amounts : order.ensure_updated_shipments
  PromotionHandler::Cart.new(order, line_item).activate
  ItemAdjustments.new(line_item).update
  reload_totals
  line_item
end

#update_cart(params) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/spree/order_contents.rb', line 29

def update_cart(params)
  if order.update_attributes(params)
    order.line_items = order.line_items.select {|li| li.quantity > 0 }
    # Update totals, then check if the order is eligible for any cart promotions.
    # If we do not update first, then the item total will be wrong and ItemTotal
    # promotion rules would not be triggered.
    reload_totals
    PromotionHandler::Cart.new(order).activate
    order.ensure_updated_shipments
    reload_totals
    true
  else
    false
  end
end