Sha256: c18b050dbc4869e744d68d7fd58f9753af76a4b09803ceea246d3ef0e28afd53

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

Spree::Order.class_eval do
  attr_accessible :coupon_code
  attr_accessor :coupon_code

  # Tells us if there if the specified promotion is already associated with the order
  # regardless of whether or not its currently eligible.  Useful because generally
  # you would only want a promotion to apply to order no more than once.
  def promotion_credit_exists?(promotion)
    !! adjustments.promotion.reload.detect { |credit| credit.originator.promotion.id == promotion.id }
  end

  unless self.method_defined?('update_adjustments_with_promotion_limiting')
    def update_adjustments_with_promotion_limiting
      update_adjustments_without_promotion_limiting
      return if adjustments.promotion.eligible.none?
      most_valuable_adjustment = adjustments.promotion.eligible.max{|a,b| a.amount.abs <=> b.amount.abs}
      current_adjustments = (adjustments.promotion.eligible - [most_valuable_adjustment])
      current_adjustments.each do |adjustment|
        if adjustment.originator.calculator.is_a?(Spree::Calculator::PerItem)
          adjustment.update_attribute_without_callbacks(:eligible, false)
        end
      end
    end
    alias_method_chain :update_adjustments, :promotion_limiting
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spree_promo-1.2.0 app/models/spree/order_decorator.rb
spree_promo-1.2.0.rc2 app/models/spree/order_decorator.rb
spree_promo-1.2.0.rc1 app/models/spree/order_decorator.rb
spree_promo-1.1.3 app/models/spree/order_decorator.rb