Class: Spree::Promo::CouponApplicator

Inherits:
Object
  • Object
show all
Defined in:
lib/spree/promo/coupon_applicator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order) ⇒ CouponApplicator

Returns a new instance of CouponApplicator.



6
7
8
# File 'lib/spree/promo/coupon_applicator.rb', line 6

def initialize(order)
  @order = order
end

Instance Attribute Details

#orderObject (readonly)

Returns the value of attribute order.



4
5
6
# File 'lib/spree/promo/coupon_applicator.rb', line 4

def order
  @order
end

Instance Method Details

#applyObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/spree/promo/coupon_applicator.rb', line 10

def apply
  if @order.coupon_code.present?
    # check if coupon code is already applied
    if @order.adjustments.promotion.eligible.detect { |p| p.originator.promotion.code == @order.coupon_code }.present?
      return { :coupon_applied? => true, :notice => Spree.t(:coupon_code_already_applied) }
    else
      promotion = Spree::Promotion.find_by_code(@order.coupon_code)
      if promotion.present?
        handle_present_promotion(promotion)
      else
        return { :coupon_applied? => false, :error => Spree.t(:coupon_code_not_found) }
      end
    end
  else
    return { :coupon_applied? => true }
  end
end