Class: Spree::Promotion
- Inherits:
-
Base
- Object
- ApplicationRecord
- Base
- Spree::Promotion
show all
- Includes:
- Metadata, MultiStoreResource, Security::Promotions, Webhooks::HasWebhooks
- Defined in:
- app/models/spree/promotion.rb,
app/models/spree/promotion/rules/user.rb,
app/models/spree/promotion/rules/taxon.rb,
app/models/spree/promotion/rules/country.rb,
app/models/spree/promotion/rules/product.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_line_items.rb,
app/models/spree/promotion/actions/create_item_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
Methods inherited from Base
belongs_to_required_by_default, for_store, has_many_inversing, json_api_columns, json_api_permitted_attributes, json_api_type, page, spree_base_scopes, spree_base_uniqueness_scope
#clear_preferences, #default_preferences, #defined_preferences, #deprecated_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_deprecated, #preference_type, #set_preference
Instance Attribute Details
#eligibility_errors ⇒ Object
Returns the value of attribute eligibility_errors.
15
16
17
|
# File 'app/models/spree/promotion.rb', line 15
def eligibility_errors
@eligibility_errors
end
|
#generate_code ⇒ Object
Returns the value of attribute generate_code.
15
16
17
|
# File 'app/models/spree/promotion.rb', line 15
def generate_code
@generate_code
end
|
Class Method Details
.active ⇒ Object
59
60
61
62
|
# File 'app/models/spree/promotion.rb', line 59
def self.active
where('spree_promotions.starts_at IS NULL OR spree_promotions.starts_at < ?', Time.current).
where('spree_promotions.expires_at IS NULL OR spree_promotions.expires_at > ?', Time.current)
end
|
.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
|
.with_coupon_code(coupon_code) ⇒ Object
53
54
55
56
57
|
# File 'app/models/spree/promotion.rb', line 53
def self.with_coupon_code(coupon_code)
where("lower(#{table_name}.code) = ?", coupon_code.strip.downcase).
includes(:promotion_actions).where.not(spree_promotion_actions: { id: nil }).
last
end
|
Instance Method Details
#activate(payload) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'app/models/spree/promotion.rb', line 78
def activate(payload)
order = payload[:order]
return unless self.class.order_activatable?(order)
payload[:promotion] = self
results = actions.map do |action|
action.perform(payload)
end
action_taken = results.include?(true)
if action_taken
orders << order
save
end
action_taken
end
|
#adjusted_credits_count(promotable) ⇒ Object
177
178
179
180
|
# File 'app/models/spree/promotion.rb', line 177
def adjusted_credits_count(promotable)
adjustments = promotable.is_a?(Order) ? promotable.all_adjustments : promotable.adjustments
credits_count - adjustments.promotion.where(source_id: actions.pluck(:id)).size
end
|
#credits ⇒ Object
182
183
184
|
# File 'app/models/spree/promotion.rb', line 182
def credits
Adjustment.eligible.promotion.where(source_id: actions.map(&:id))
end
|
#credits_count ⇒ Object
186
187
188
|
# File 'app/models/spree/promotion.rb', line 186
def credits_count
credits.count
end
|
#deactivate(payload) ⇒ Object
Called when a promotion is removed from the cart
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
|
# File 'app/models/spree/promotion.rb', line 104
def deactivate(payload)
order = payload[:order]
return unless self.class.order_activatable?(order)
payload[:promotion] = self
results = actions.map do |action|
action.revert(payload) if action.respond_to?(:revert)
end
action_taken = results.include?(true)
if action_taken
orders << order
save
end
action_taken
end
|
#eligible?(promotable) ⇒ Boolean
called anytime order.update_with_updater! happens
131
132
133
134
135
|
# File 'app/models/spree/promotion.rb', line 131
def eligible?(promotable)
return false if expired? || usage_limit_exceeded?(promotable) || blacklisted?(promotable)
!!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)
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# File 'app/models/spree/promotion.rb', line 140
def eligible_rules(promotable, options = {})
return [] if rules.none?
specific_rules = rules.select { |rule| rule.applicable?(promotable) }
return [] if specific_rules.none?
rule_eligibility = Hash[specific_rules.map do |rule|
[rule, rule.eligible?(promotable, options)]
end]
if match_all?
unless rule_eligibility.values.all?
@eligibility_errors = specific_rules.map(&:eligibility_errors).detect(&:present?)
return nil
end
specific_rules
else
unless rule_eligibility.values.any?
@eligibility_errors = specific_rules.map(&:eligibility_errors).detect(&:present?)
return nil
end
[rule_eligibility.detect { |_, eligibility| eligibility }.first]
end
end
|
#expired? ⇒ Boolean
74
75
76
|
# File 'app/models/spree/promotion.rb', line 74
def expired?
!!(starts_at && Time.current < starts_at || expires_at && Time.current > expires_at)
end
|
#line_item_actionable?(order, line_item) ⇒ Boolean
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
# File 'app/models/spree/promotion.rb', line 190
def line_item_actionable?(order, line_item)
if eligible? order
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
|
#products ⇒ Object
169
170
171
|
# File 'app/models/spree/promotion.rb', line 169
def products
rules.where(type: 'Spree::Promotion::Rules::Product').map(&:products).flatten.uniq
end
|
#usage_limit_exceeded?(promotable) ⇒ Boolean
173
174
175
|
# File 'app/models/spree/promotion.rb', line 173
def usage_limit_exceeded?(promotable)
usage_limit.present? && usage_limit > 0 && adjusted_credits_count(promotable) >= usage_limit
end
|
#used_by?(user, excluded_orders = []) ⇒ Boolean
205
206
207
208
209
210
|
# File 'app/models/spree/promotion.rb', line 205
def used_by?(user, excluded_orders = [])
user.orders.complete.joins(:promotions).joins(:all_adjustments).
where.not(spree_orders: { id: excluded_orders.map(&:id) }).
where(spree_promotions: { id: id }).
where(spree_adjustments: { source_type: 'Spree::PromotionAction', eligible: true }).any?
end
|