Sha256: d9f8ac2b57d1af89363007dce16d3ae3b85ad3104265bf31fec779706ce057ff

Contents?: true

Size: 1.45 KB

Versions: 15

Compression:

Stored size: 1.45 KB

Contents

require_dependency 'spree/calculator'

module Spree
  class Calculator::PerItem < Calculator
    preference :amount, :decimal, :default => 0
    preference :currency, :string, :default => Spree::Config[:currency]

    attr_accessible :preferred_amount, :preferred_currency

    def self.description
      Spree.t(:flat_rate_per_item)
    end

    def compute(object=nil)
      return 0 if object.nil?
      self.preferred_amount * object.line_items.reduce(0) do |sum, value|
        if matching_products.blank? || matching_products.include?(value.product)
          value_to_add = value.quantity
        else
          value_to_add = 0
        end
        sum + value_to_add
      end
    end

    # Returns all products that match this calculator, but only if the calculator
    # is attached to a promotion. If attached to a ShippingMethod, nil is returned.
    def matching_products
      # Regression check for #1596
      # Calculator::PerItem can be used in two cases.
      # The first is in a typical promotion, providing a discount per item of a particular item
      # The second is a ShippingMethod, where it applies to an entire order
      #
      # Shipping methods do not have promotions attached, but promotions do
      # Therefore we must check for promotions
      if self.calculable.respond_to?(:promotion)
        self.calculable.promotion.rules.map do |rule|
          rule.respond_to?(:products) ? rule.products : []
        end.flatten
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
spree_core-2.0.13 app/models/spree/calculator/per_item.rb
spree_core-2.0.12 app/models/spree/calculator/per_item.rb
spree_core-2.0.11 app/models/spree/calculator/per_item.rb
spree_core-2.0.10 app/models/spree/calculator/per_item.rb
spree_core-2.0.9 app/models/spree/calculator/per_item.rb
spree_core-2.0.8 app/models/spree/calculator/per_item.rb
spree_core-2.0.7 app/models/spree/calculator/per_item.rb
spree_core-2.0.6 app/models/spree/calculator/per_item.rb
spree_core-2.0.5 app/models/spree/calculator/per_item.rb
spree_core-2.0.4 app/models/spree/calculator/per_item.rb
spree_core-2.0.3 app/models/spree/calculator/per_item.rb
spree_core-2.0.2 app/models/spree/calculator/per_item.rb
spree_core-2.0.1 app/models/spree/calculator/per_item.rb
spree_core-2.0.0 app/models/spree/calculator/per_item.rb
spree_core-2.0.0.rc1 app/models/spree/calculator/per_item.rb