Class: Spree::Promotion

Inherits:
Base
  • Object
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/minimum_quantity.rb,
app/models/spree/promotion/rules/one_use_per_user.rb,
app/models/spree/promotion/actions/create_adjustment.rb,
app/models/spree/promotion/order_adjustments_recalculator.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 Classes: OrderAdjustmentsRecalculator

Constant Summary collapse

UNACTIVATABLE_ORDER_STATES =
["complete", "awaiting_return", "returned"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#eligibility_errorsObject (readonly)

Returns the value of attribute eligibility_errors.



7
8
9
# File 'app/models/spree/promotion.rb', line 7

def eligibility_errors
  @eligibility_errors
end

Class Method Details

.order_activatable?(order) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/models/spree/promotion.rb', line 57

def self.order_activatable?(order)
  order && !UNACTIVATABLE_ORDER_STATES.include?(order.state)
end

.with_coupon_code(val) ⇒ Object



61
62
63
64
65
# File 'app/models/spree/promotion.rb', line 61

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



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
137
138
139
140
141
142
# File 'app/models/spree/promotion.rb', line 109

def activate(order:, line_item: nil, user: nil, path: nil, promotion_code: nil)
  return unless self.class.order_activatable?(order)

  payload = {
    order:,
    promotion: self,
    line_item:,
    user:,
    path:,
    promotion_code:
  }

  # 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
    order.order_promotions.find_or_create_by!(
      promotion: self,
      promotion_code:,
    )
    order.promotions.reset
    order_promotions.reset
    orders.reset
  end

  action_taken
end

#active?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'app/models/spree/promotion.rb', line 101

def active?
  started? && not_expired? && actions.present?
end

#as_json(options = {}) ⇒ Object



80
81
82
83
# File 'app/models/spree/promotion.rb', line 80

def as_json(options = {})
  options[:except] ||= :code
  super
end

#discounted_ordersObject

All orders that have been discounted using this promotion



68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/spree/promotion.rb', line 68

def discounted_orders
  Spree::Order.
    joins(:all_adjustments).
    where(
      spree_adjustments: {
        source_type: "Spree::PromotionAction",
        source_id: actions.map(&:id),
        eligible: true
      }
    ).distinct
end

#eligible?(promotable, promotion_code: nil) ⇒ Boolean

called anytime order.recalculate happens

Returns:

  • (Boolean)


145
146
147
148
149
150
151
152
153
154
# File 'app/models/spree/promotion.rb', line 145

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:)
  return false if promotion_code&.usage_limit_exceeded?(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)



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/models/spree/promotion.rb', line 159

def eligible_rules(promotable, options = {})
  # Promotions without rules are eligible by default.
  return [] if rules.none?

  eligible = lambda { |rule| rule.eligible?(promotable, options) }
  specific_rules = rules.select { |rule| rule.applicable?(promotable) }
  return [] if specific_rules.none?

  # If there are rules for this promotion, but no rules for this
  # particular promotable, then the promotion is ineligible by default.
  unless specific_rules.all?(&eligible)
    @eligibility_errors = specific_rules.map(&:eligibility_errors).detect(&:present?)
    return nil
  end
  specific_rules
end

#expired?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'app/models/spree/promotion.rb', line 93

def expired?
  expires_at.present? && expires_at < Time.current
end

#inactive?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'app/models/spree/promotion.rb', line 105

def inactive?
  !active?
end

#line_item_actionable?(order, line_item, promotion_code: nil) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/models/spree/promotion.rb', line 202

def line_item_actionable?(order, line_item, promotion_code: nil)
  return false if blacklisted?(line_item)

  if eligible?(order, promotion_code:)
    rules = eligible_rules(order)
    if rules.blank?
      true
    else
      rules.all? { |rule| rule.actionable? line_item }
    end
  else
    false
  end
end

#not_expired?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'app/models/spree/promotion.rb', line 97

def not_expired?
  !expired?
end

#not_started?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'app/models/spree/promotion.rb', line 85

def not_started?
  !started?
end

#productsObject



176
177
178
# File 'app/models/spree/promotion.rb', line 176

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.

Parameters:

  • order (Spree::Order)

    the order to remove the promotion from.



230
231
232
233
234
235
236
237
238
# File 'app/models/spree/promotion.rb', line 230

def remove_from(order)
  actions.each do |action|
    action.remove_from(order)
  end
  # NOTE: this destroys the join table entry, not the promotion itself
  order.promotions.destroy(self)
  order.order_promotions.reset
  order_promotions.reset
end

#started?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'app/models/spree/promotion.rb', line 89

def started?
  starts_at.nil? || starts_at < Time.current
end

#usage_count(excluded_orders: []) ⇒ Integer

Number of times the code has been used overall

Parameters:

  • excluded_orders (Array<Spree::Order>) (defaults to: [])

    Orders to exclude from usage count

Returns:

  • (Integer)

    usage count



194
195
196
197
198
199
200
# File 'app/models/spree/promotion.rb', line 194

def usage_count(excluded_orders: [])
  discounted_orders.
    complete.
    where.not(id: [excluded_orders.map(&:id)]).
    where.not(spree_orders: { state: :canceled }).
    count
end

#usage_limit_exceeded?(excluded_orders: []) ⇒ Boolean

Whether the promotion has exceeded its usage restrictions.

Parameters:

  • excluded_orders (Array<Spree::Order>) (defaults to: [])

    Orders to exclude from usage limit

Returns:

  • (Boolean)

    true or false



184
185
186
187
188
# File 'app/models/spree/promotion.rb', line 184

def usage_limit_exceeded?(excluded_orders: [])
  if usage_limit
    usage_count(excluded_orders:) >= usage_limit
  end
end

#used_by?(user, excluded_orders = []) ⇒ Boolean

Returns:

  • (Boolean)


217
218
219
220
221
222
223
224
# File 'app/models/spree/promotion.rb', line 217

def used_by?(user, excluded_orders = [])
  discounted_orders.
    complete.
    where.not(id: excluded_orders.map(&:id)).
    where(user:).
    where.not(spree_orders: { state: :canceled }).
    exists?
end