Sha256: e19bf4e13b60b5af083048c93132946855763aa7d5dc6a23568b8f755593fdd4

Contents?: true

Size: 1.6 KB

Versions: 14

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

require_dependency 'spree/calculator'

module Spree
  # A calculator for promotions that calculates a percent-off discount
  # for all matching products in an order. This should not be used as a
  # shipping calculator since it would be the same thing as a flat percent
  # off the entire order.
  #
  #
  # TODO Should be deprecated now that we have adjustments at the line item level in spree core

  class Calculator::PercentPerItem < Calculator
    preference :percent, :decimal, default: 0

    def compute(object = nil)
      Spree::Deprecation.warn('This method is deprecated, please use adjustments at line item level')

      return 0 if object.nil?
      object.line_items.sum { |line_item|
        value_for_line_item(line_item)
      }
    end

    private

    # 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.
    # Copied from per_item.rb
    def matching_products
      if compute_on_promotion?
        calculable.promotion.rules.flat_map do |rule|
          rule.respond_to?(:products) ? rule.products : []
        end
      end
    end

    def value_for_line_item(line_item)
      if compute_on_promotion?
        return 0 unless matching_products.blank? || matching_products.include?(line_item.product)
      end
      ((line_item.price * line_item.quantity) * preferred_percent) / 100
    end

    # Determines wether or not the calculable object is a promotion
    def compute_on_promotion?
      @compute_on_promotion ||= calculable.respond_to?(:promotion)
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
solidus_core-2.11.17 app/models/spree/calculator/percent_per_item.rb
solidus_core-2.11.16 app/models/spree/calculator/percent_per_item.rb
solidus_core-2.11.15 app/models/spree/calculator/percent_per_item.rb
solidus_core-2.11.14 app/models/spree/calculator/percent_per_item.rb
solidus_core-2.11.13 app/models/spree/calculator/percent_per_item.rb
solidus_core-2.11.12 app/models/spree/calculator/percent_per_item.rb
solidus_core-2.11.11 app/models/spree/calculator/percent_per_item.rb
solidus_core-2.11.10 app/models/spree/calculator/percent_per_item.rb
solidus_core-2.11.9 app/models/spree/calculator/percent_per_item.rb
solidus_core-2.11.8 app/models/spree/calculator/percent_per_item.rb
solidus_core-2.11.7 app/models/spree/calculator/percent_per_item.rb
solidus_core-2.11.6 app/models/spree/calculator/percent_per_item.rb
solidus_core-2.11.5 app/models/spree/calculator/percent_per_item.rb
solidus_core-2.11.4 app/models/spree/calculator/percent_per_item.rb