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

- (OrderContents) initialize(order)

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

- (Object) order

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

- (Spree::LineItem) add(variant, quantity = 1, options = {})

Add a line items to the order if there is inventory to do so and populate Promotions

Parameters:

  • :options (Hash)

    Options for the adding proccess Valid options:

    shipment: [Spree::Shipment] LineItem target shipment
    stock_location_quantities:
      stock_location_id: The stock location to source from

Returns:



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

def add(variant, quantity = 1, options = {})
  line_item = add_to_line_item(variant, quantity, options)
  after_add_or_remove(line_item, options)
end

- (Object) advance



55
56
57
# File 'app/models/spree/order_contents.rb', line 55

def advance
  while @order.next; end
end

- (Object) approve(user: nil, name: nil)



59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/spree/order_contents.rb', line 59

def approve(user: nil, name: nil)
  if user.blank? && name.blank?
    raise ArgumentError, 'user or name must be specified'
  end

  order.update_attributes!(
    approver: user,
    approver_name: name,
    approved_at: Time.current
  )
end

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



27
28
29
30
# File 'app/models/spree/order_contents.rb', line 27

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

- (Object) remove_line_item(line_item, options = {})



32
33
34
35
# File 'app/models/spree/order_contents.rb', line 32

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

- (Object) update_cart(params)



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/spree/order_contents.rb', line 37

def update_cart(params)
  if order.update_attributes(params)
    unless order.completed?
      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
    end
    reload_totals
    true
  else
    false
  end
end