Sha256: 9eeae70c423b026e4847f7574b8a4e00ab4846c861b0abeba893880fc38153c7

Contents?: true

Size: 1.8 KB

Versions: 7

Compression:

Stored size: 1.8 KB

Contents

Spree::StoreController.class_eval do

  protected
    def apply_coupon_code
      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?
          flash[:notice] = t(:coupon_code_already_applied)
          true
        else
          event_name = "spree.checkout.coupon_code_added"

          # TODO should restrict to payload's event name?
          promotion = Spree::Promotion.find_by_code(@order.coupon_code)

          if promotion.present?
            if promotion.expired?
              flash[:error] = t(:coupon_code_expired)
              return false
            end

            if promotion.usage_limit_exceeded?
              flash[:error] = t(:coupon_code_max_usage)
              return false
            end

            previous_promo = @order.adjustments.promotion.eligible.first
            fire_event(event_name, :coupon_code => @order.coupon_code)
            promo = @order.adjustments.promotion.detect { |p| p.originator.promotion.code == @order.coupon_code }

            if promo.present? and promo.eligible
              flash[:success] = t(:coupon_code_applied)
              true
            elsif previous_promo.present? and promo.present?
              flash[:error] = t(:coupon_code_better_exists)
              false
            elsif promo.present?
              flash[:error] = t(:coupon_code_not_eligible)
              false
            else
              # if the promotion was created after the order
              flash[:error] = t(:coupon_code_not_found)
              false
            end
          else
            flash[:error] = t(:coupon_code_not_found)
            false
          end
        end
      else
        true
      end
    end

end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
spree_promo-1.3.3 app/controllers/spree/store_controller_decorator.rb
spree_promo-1.3.2 app/controllers/spree/store_controller_decorator.rb
spree_promo-1.3.1 app/controllers/spree/store_controller_decorator.rb
spree_promo-1.3.0 app/controllers/spree/store_controller_decorator.rb
spree_promo-1.3.0.rc2 app/controllers/spree/store_controller_decorator.rb
dup_spree_promo-1.3.0.rc1 app/controllers/spree/store_controller_decorator.rb
spree_promo-1.3.0.rc1 app/controllers/spree/store_controller_decorator.rb