Class: Spree::Promotion
- Inherits:
-
Object
- Object
- Spree::Promotion
- Includes:
- Metadata, Metafields, MultiStoreResource, Security::Promotions
- 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/currency.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/customer_group.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
Constant Summary collapse
- MATCH_POLICIES =
%w(all any)- UNACTIVATABLE_ORDER_STATES =
['complete', 'awaiting_return', 'returned']
Instance Attribute Summary collapse
-
#eligibility_errors ⇒ Object
readonly
Returns the value of attribute eligibility_errors.
-
#generate_code ⇒ Object
Returns the value of attribute generate_code.
Class Method Summary collapse
Instance Method Summary collapse
- #activate(payload) ⇒ Object
- #active? ⇒ Boolean
- #adjusted_credits_count(promotable) ⇒ Object
- #all_codes_used? ⇒ Boolean
-
#cached_rules ⇒ Array<Spree::PromotionRule>
We cache the rules to avoid multiple database queries this is useful for orders with many line items.
- #code_for_order(order) ⇒ Object
- #credits ⇒ Object
- #credits_count ⇒ Object
-
#deactivate(payload) ⇒ Object
Called when a promotion is removed from the cart.
-
#eligible?(promotable, options = {}) ⇒ Boolean
called anytime order.update_with_updater! 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) ⇒ Boolean
- #name_for_order(order) ⇒ Object
- #products ⇒ Object
- #usage_limit_exceeded?(promotable) ⇒ Boolean
- #used_by?(user, excluded_orders = []) ⇒ Boolean
Instance Attribute Details
#eligibility_errors ⇒ Object (readonly)
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
101 102 103 104 |
# File 'app/models/spree/promotion.rb', line 101 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
106 107 108 |
# File 'app/models/spree/promotion.rb', line 106 def self.order_activatable?(order) order && !UNACTIVATABLE_ORDER_STATES.include?(order.state) end |
.with_coupon_code(coupon_code) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'app/models/spree/promotion.rb', line 89 def self.with_coupon_code(coupon_code) return nil unless coupon_code.present? coupon_code = coupon_code.strip.downcase coupons.includes(:promotion_actions). where.not(spree_promotion_actions: { id: nil }). where(code: coupon_code).or( where(id: Spree::CouponCode.where(code: coupon_code).select(:promotion_id)) ).last end |
Instance Method Details
#activate(payload) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'app/models/spree/promotion.rb', line 132 def activate(payload) order = payload[:order] return unless self.class.order_activatable?(order) payload[:promotion] = self # Track results from actions to see if any action has been taken. # Actions should return nil/false if no action has been taken. # If an action returns true, then an action has been taken. results = actions.map do |action| action.perform(payload) end # If an action has been taken, report back to whatever activated this promotion. action_taken = results.include?(true) if action_taken # connect to the order # create the join_table entry. order.promotions << self unless order.promotions.include?(self) order.save end action_taken end |
#active? ⇒ Boolean
116 117 118 |
# File 'app/models/spree/promotion.rb', line 116 def active? starts_at.present? && starts_at < Time.current && (expires_at.blank? || !expired?) end |
#adjusted_credits_count(promotable) ⇒ Object
243 244 245 246 |
# File 'app/models/spree/promotion.rb', line 243 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 |
#all_codes_used? ⇒ Boolean
128 129 130 |
# File 'app/models/spree/promotion.rb', line 128 def all_codes_used? coupon_codes.used.count == coupon_codes.count end |
#cached_rules ⇒ Array<Spree::PromotionRule>
We cache the rules to avoid multiple database queries this is useful for orders with many line items
195 196 197 198 199 200 201 |
# File 'app/models/spree/promotion.rb', line 195 def cached_rules Rails.cache.fetch("#{cache_key_with_version}/rules") do rules.to_a end rescue TypeError # when using null_store in test environment rules.to_a end |
#code_for_order(order) ⇒ Object
286 287 288 289 290 291 292 |
# File 'app/models/spree/promotion.rb', line 286 def code_for_order(order) if multi_codes? coupon_codes.find_by(order: order)&.code else code end end |
#credits ⇒ Object
248 249 250 |
# File 'app/models/spree/promotion.rb', line 248 def credits Adjustment.eligible.promotion.where(source_id: actions.map(&:id)) end |
#credits_count ⇒ Object
252 253 254 |
# File 'app/models/spree/promotion.rb', line 252 def credits_count credits.count end |
#deactivate(payload) ⇒ Object
Called when a promotion is removed from the cart
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'app/models/spree/promotion.rb', line 158 def deactivate(payload) order = payload[:order] return unless self.class.order_activatable?(order) payload[:promotion] = self # Track results from actions to see if any action has been taken. # Actions should return nil/false if no action has been taken. # If an action returns true, then an action has been taken. results = actions.map do |action| action.revert(payload) if action.respond_to?(:revert) end # If an action has been taken, report back to whatever `d this promotion. action_taken = results.include?(true) if action_taken # connect to the order # create the join_table entry. order.promotions << self unless order.promotions.include?(self) order.save end action_taken end |
#eligible?(promotable, options = {}) ⇒ Boolean
called anytime order.update_with_updater! happens
185 186 187 188 189 |
# File 'app/models/spree/promotion.rb', line 185 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)
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'app/models/spree/promotion.rb', line 206 def eligible_rules(promotable, = {}) # Promotions without rules are eligible by default. return [] if cached_rules.none? specific_rules = cached_rules.select { |rule| rule.applicable?(promotable) } return [] if specific_rules.none? rule_eligibility = Hash[specific_rules.map do |rule| [rule, rule.eligible?(promotable, )] end] if match_all? # If there are rules for this promotion, but no rules for this # particular promotable, then the promotion is ineligible by default. 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
124 125 126 |
# File 'app/models/spree/promotion.rb', line 124 def expired? !!(starts_at && Time.current < starts_at || expires_at && Time.current > expires_at) end |
#inactive? ⇒ Boolean
120 121 122 |
# File 'app/models/spree/promotion.rb', line 120 def inactive? !active? end |
#line_item_actionable?(order, line_item) ⇒ Boolean
256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'app/models/spree/promotion.rb', line 256 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 |
#name_for_order(order) ⇒ Object
278 279 280 281 282 283 284 |
# File 'app/models/spree/promotion.rb', line 278 def name_for_order(order) if coupon_code? code_for_order(order) else name end.to_s.upcase end |
#products ⇒ Object
235 236 237 |
# File 'app/models/spree/promotion.rb', line 235 def products rules.where(type: 'Spree::Promotion::Rules::Product').map(&:products).flatten.uniq end |
#usage_limit_exceeded?(promotable) ⇒ Boolean
239 240 241 |
# File 'app/models/spree/promotion.rb', line 239 def usage_limit_exceeded?(promotable) usage_limit.present? && usage_limit > 0 && adjusted_credits_count(promotable) >= usage_limit end |
#used_by?(user, excluded_orders = []) ⇒ Boolean
271 272 273 274 275 276 |
# File 'app/models/spree/promotion.rb', line 271 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 |