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


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, options = {}) ⇒ Object


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

def add(variant, quantity = 1, options = {})
  timestamp = Time.current
  line_item = add_to_line_item(variant, quantity, options)
  options[:line_item_created] = true if timestamp <= line_item.created_at
  after_add_or_remove(line_item, options)
end

#remove(variant, quantity = 1, options = {}) ⇒ Object


16
17
18
19
# File 'app/models/spree/order_contents.rb', line 16

def remove(variant, quantity = 1, options = {})
  line_item = remove_from_line_item(variant, quantity, options)
  after_add_or_remove(line_item, options)
end

#remove_line_item(line_item, options = {}) ⇒ Object


21
22
23
24
# File 'app/models/spree/order_contents.rb', line 21

def remove_line_item(line_item, options = {})
  line_item.destroy!
  after_add_or_remove(line_item, options)
end

#update_cart(params) ⇒ Object


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

def update_cart(params)
  if order.update_attributes(filter_order_items(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.
    persist_totals
    PromotionHandler::Cart.new(order).activate
    order.ensure_updated_shipments
    order.payments.store_credits.checkout.destroy_all
    persist_totals
    true
  else
    false
  end
end