Class: Spree::PromotionCode

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree/promotion_code.rb

Defined Under Namespace

Classes: BatchBuilder

Instance Method Summary collapse

Methods inherited from Base

display_includes

Methods included from Core::Permalinks

#generate_permalink, #save_permalink

Instance Method Details

#promotion_not_apply_automaticallyObject


41
42
43
# File 'app/models/spree/promotion_code.rb', line 41

def promotion_not_apply_automatically
  errors.add(:base, :disallowed_with_apply_automatically) if promotion.apply_automatically
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


30
31
32
33
34
35
# File 'app/models/spree/promotion_code.rb', line 30

def usage_count(excluded_orders: [])
  adjustments.
  eligible.
  in_completed_orders(excluded_orders: excluded_orders, exclude_canceled: true).
  count(:order_id)
end

#usage_limitObject


37
38
39
# File 'app/models/spree/promotion_code.rb', line 37

def usage_limit
  promotion.per_code_usage_limit
end

#usage_limit_exceeded?(excluded_orders: []) ⇒ Boolean

Whether the promotion code has exceeded its usage restrictions

Parameters:

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

    Orders to exclude from usage limit

Returns:

  • (Boolean)

    true or false


20
21
22
23
24
# File 'app/models/spree/promotion_code.rb', line 20

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