/
order_populator.rb
57 lines (49 loc) 路 1.53 KB
/
order_populator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module Spree
class OrderPopulator
attr_accessor :order, :currency
attr_reader :errors
def initialize(order, currency)
@order = order
@currency = currency
@errors = ActiveModel::Errors.new(self)
end
#
# Parameters can be passed using the following possible parameter configurations:
#
# * Single variant/quantity pairing
# +:variants => { variant_id => quantity }+
#
# * Multiple products at once
# +:products => { product_id => variant_id, product_id => variant_id }, :quantity => quantity+
def populate(from_hash)
from_hash[:products].each do |product_id,variant_id|
attempt_cart_add(variant_id, from_hash[:quantity])
end if from_hash[:products]
from_hash[:variants].each do |variant_id, quantity|
attempt_cart_add(variant_id, quantity)
end if from_hash[:variants]
valid?
end
def valid?
errors.empty?
end
private
def attempt_cart_add(variant_id, quantity)
quantity = quantity.to_i
# 2,147,483,647 is crazy.
# See issue #2695.
if quantity > 2_147_483_647
errors.add(:base, Spree.t(:please_enter_reasonable_quantity, :scope => :order_populator))
return false
end
variant = Spree::Variant.find(variant_id)
if quantity > 0
line_item = @order.contents.add(variant, quantity, currency)
unless line_item.valid?
errors.add(:base, line_item.errors.messages.values.join(" "))
return false
end
end
end
end
end