-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmoney_discount.rb
59 lines (54 loc) · 1.41 KB
/
money_discount.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
58
59
# MoneyDiscount
# =============
#
# The `MoneyDiscount` gives flat amount discounts.
#
# Example
# -------
# * $5 off
#
class MoneyDiscount
# Initializes the discount.
#
# Arguments
# ---------
#
# * cents
# The discount amount (in cents).
#
# * message
# The message to show for the discount.
#
def initialize(cents, message)
@amount = Money.new(cents: cents)
@message = message
end
# Applies the discount on a line item.
#
# Arguments
# ---------
#
# * line_item
# The item to which the discount will be applied.
#
# Example
# -------
# Given `MoneyDiscount.new(5_00, "Great discount")` and the following line item:
#
# * Quantity = 2, Price = 10
#
# The discount will give $5 off per quantity, for a total of 10$ off.
#
def apply(line_item)
# Calculate the total discount for this line item
line_discount = @amount * line_item.quantity
# Calculated the discounted line price
new_line_price = line_item.line_price - line_discount
# Apply the new line price to this line item with a given message
# describing the discount, which may be displayed in cart pages and
# confirmation emails to describe the applied discount.
line_item.change_line_price(new_line_price, message: @message)
# Print a debugging line to the console
puts "Discounted line item with variant #{line_item.variant.id} by #{line_discount}."
end
end