Class: Spree::Promotion
- Inherits:
-
Base
- Object
- ActiveRecord::Base
- Base
- Spree::Promotion
show all
- Defined in:
- app/models/spree/promotion.rb,
app/models/spree/promotion/rules/user.rb,
app/models/spree/promotion/rules/store.rb,
app/models/spree/promotion/rules/taxon.rb,
app/models/spree/promotion/rules/product.rb,
app/models/spree/promotion/rules/nth_order.rb,
app/models/spree/promotion/rules/user_role.rb,
app/models/spree/promotion/rules/item_total.rb,
app/models/spree/promotion/rules/first_order.rb,
app/models/spree/promotion/rules/option_value.rb,
app/models/spree/promotion/rules/user_logged_in.rb,
app/models/spree/promotion/actions/free_shipping.rb,
app/models/spree/promotion/rules/one_use_per_user.rb,
app/models/spree/promotion/actions/create_adjustment.rb,
app/models/spree/promotion/actions/create_item_adjustments.rb,
app/models/spree/promotion/rules/first_repeat_purchase_since.rb,
app/models/spree/promotion/actions/create_quantity_adjustments.rb
Defined Under Namespace
Modules: Actions, Rules
Constant Summary
collapse
- MATCH_POLICIES =
%w(all any)
- UNACTIVATABLE_ORDER_STATES =
["complete", "awaiting_return", "returned"]
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#activate(order:, line_item: nil, user: nil, path: nil, promotion_code: nil) ⇒ Object
-
#active? ⇒ Boolean
-
#as_json(options = {}) ⇒ Object
-
#eligible?(promotable, promotion_code: nil) ⇒ Boolean
called anytime order.recalculate happens.
-
#eligible_rules(promotable, options = {}) ⇒ Object
eligible_rules returns an array of promotion rules where eligible? is true for the promotable if there are no such rules, an empty array is returned if the rules make this promotable ineligible, then nil is returned (i.e. this promotable is not eligible).
-
#expired? ⇒ Boolean
-
#inactive? ⇒ Boolean
-
#line_item_actionable?(order, line_item, promotion_code: nil) ⇒ Boolean
-
#not_expired? ⇒ Boolean
-
#not_started? ⇒ Boolean
-
#products ⇒ Object
-
#remove_from(order) ⇒ void
Removes a promotion and any adjustments or other side effects from an order.
-
#started? ⇒ Boolean
-
#usage_count(excluded_orders: []) ⇒ Integer
Number of times the code has been used overall.
-
#usage_limit_exceeded?(excluded_orders: []) ⇒ Boolean
Whether the promotion has exceeded its usage restrictions.
-
#used_by?(user, excluded_orders = []) ⇒ Boolean
Methods inherited from Base
display_includes, page, preference, #preferences
#generate_permalink, #save_permalink
Instance Attribute Details
#eligibility_errors ⇒ Object
Returns the value of attribute eligibility_errors.
8
9
10
|
# File 'app/models/spree/promotion.rb', line 8
def eligibility_errors
@eligibility_errors
end
|
Class Method Details
.order_activatable?(order) ⇒ Boolean
64
65
66
|
# File 'app/models/spree/promotion.rb', line 64
def self.order_activatable?(order)
order && !UNACTIVATABLE_ORDER_STATES.include?(order.state)
end
|
.ransackable_scopes ⇒ Object
60
61
62
|
# File 'app/models/spree/promotion.rb', line 60
def self.ransackable_scopes(*)
%i(active)
end
|
.with_coupon_code(val) ⇒ Object
68
69
70
71
72
|
# File 'app/models/spree/promotion.rb', line 68
def self.with_coupon_code(val)
joins(:codes).where(
PromotionCode.arel_table[:value].eq(val.downcase)
).first
end
|
Instance Method Details
#activate(order:, line_item: nil, user: nil, path: nil, promotion_code: nil) ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'app/models/spree/promotion.rb', line 103
def activate(order:, line_item: nil, user: nil, path: nil, promotion_code: nil)
return unless self.class.order_activatable?(order)
payload = {
order: order,
promotion: self,
line_item: line_item,
user: user,
path: path,
promotion_code: promotion_code
}
results = actions.map do |action|
action.perform(payload)
end
action_taken = results.include?(true)
if action_taken
order.order_promotions.find_or_create_by!(
promotion: self,
promotion_code: promotion_code,
)
order.promotions.reset
order_promotions.reset
orders.reset
end
action_taken
end
|
#active? ⇒ Boolean
95
96
97
|
# File 'app/models/spree/promotion.rb', line 95
def active?
started? && not_expired? && (Spree::Config.consider_actionless_promotion_active || actions.present?)
end
|
#as_json(options = {}) ⇒ Object
74
75
76
77
|
# File 'app/models/spree/promotion.rb', line 74
def as_json(options = {})
options[:except] ||= :code
super
end
|
#eligible?(promotable, promotion_code: nil) ⇒ Boolean
called anytime order.recalculate happens
139
140
141
142
143
144
145
146
147
148
|
# File 'app/models/spree/promotion.rb', line 139
def eligible?(promotable, promotion_code: nil)
return false if inactive?
return false if blacklisted?(promotable)
excluded_orders = eligibility_excluded_orders(promotable)
return false if usage_limit_exceeded?(excluded_orders: excluded_orders)
return false if promotion_code&.usage_limit_exceeded?(excluded_orders: excluded_orders)
!!eligible_rules(promotable, {})
end
|
#eligible_rules(promotable, options = {}) ⇒ Object
eligible_rules returns an array of promotion rules where eligible? is true for the promotable if there are no such rules, an empty array is returned if the rules make this promotable ineligible, then nil is returned (i.e. this promotable is not eligible)
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'app/models/spree/promotion.rb', line 153
def eligible_rules(promotable, options = {})
return [] if rules.none?
eligible = lambda { |rule| rule.eligible?(promotable, options) }
specific_rules = rules.for(promotable)
return [] if specific_rules.none?
if match_all?
unless specific_rules.all?(&eligible)
@eligibility_errors = specific_rules.map(&:eligibility_errors).detect(&:present?)
return nil
end
specific_rules
else
unless specific_rules.any?(&eligible)
@eligibility_errors = specific_rules.map(&:eligibility_errors).detect(&:present?)
return nil
end
specific_rules.select(&eligible)
end
end
|
#expired? ⇒ Boolean
87
88
89
|
# File 'app/models/spree/promotion.rb', line 87
def expired?
expires_at.present? && expires_at < Time.current
end
|
#inactive? ⇒ Boolean
99
100
101
|
# File 'app/models/spree/promotion.rb', line 99
def inactive?
!active?
end
|
#line_item_actionable?(order, line_item, promotion_code: nil) ⇒ Boolean
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
# File 'app/models/spree/promotion.rb', line 204
def line_item_actionable?(order, line_item, promotion_code: nil)
return false if blacklisted?(line_item)
if eligible?(order, promotion_code: promotion_code)
rules = eligible_rules(order)
if rules.blank?
true
else
rules.send(match_all? ? :all? : :any?) do |rule|
rule.actionable? line_item
end
end
else
false
end
end
|
#not_expired? ⇒ Boolean
91
92
93
|
# File 'app/models/spree/promotion.rb', line 91
def not_expired?
!expired?
end
|
#not_started? ⇒ Boolean
79
80
81
|
# File 'app/models/spree/promotion.rb', line 79
def not_started?
!started?
end
|
#products ⇒ Object
178
179
180
|
# File 'app/models/spree/promotion.rb', line 178
def products
rules.where(type: "Spree::Promotion::Rules::Product").flat_map(&:products).uniq
end
|
#remove_from(order) ⇒ void
This method returns an undefined value.
Removes a promotion and any adjustments or other side effects from an order.
243
244
245
246
247
248
249
250
251
|
# File 'app/models/spree/promotion.rb', line 243
def remove_from(order)
actions.each do |action|
action.remove_from(order)
end
order.promotions.destroy(self)
order.order_promotions.reset
order_promotions.reset
end
|
#started? ⇒ Boolean
83
84
85
|
# File 'app/models/spree/promotion.rb', line 83
def started?
starts_at.nil? || starts_at < Time.current
end
|
#usage_count(excluded_orders: []) ⇒ Integer
Number of times the code has been used overall
196
197
198
199
200
201
202
|
# File 'app/models/spree/promotion.rb', line 196
def usage_count(excluded_orders: [])
Spree::Adjustment.promotion.
eligible.
in_completed_orders(excluded_orders: excluded_orders).
where(source_id: actions).
count(:order_id)
end
|
#usage_limit_exceeded?(excluded_orders: []) ⇒ Boolean
Whether the promotion has exceeded its usage restrictions.
186
187
188
189
190
|
# File 'app/models/spree/promotion.rb', line 186
def usage_limit_exceeded?(excluded_orders: [])
if usage_limit
usage_count(excluded_orders: excluded_orders) >= usage_limit
end
end
|
#used_by?(user, excluded_orders = []) ⇒ Boolean
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
# File 'app/models/spree/promotion.rb', line 221
def used_by?(user, excluded_orders = [])
[
:adjustments,
:line_item_adjustments,
:shipment_adjustments
].any? do |adjustment_type|
user.orders.complete.joins(adjustment_type).where(
spree_adjustments: {
source_type: "Spree::PromotionAction",
source_id: actions.map(&:id),
eligible: true
}
).where.not(
id: excluded_orders.map(&:id)
).any?
end
end
|