Sha256: bdab5ce103361d3b077d37abd16abd2292e5d2384cb91160c1b84cd210d39047

Contents?: true

Size: 1004 Bytes

Versions: 17

Compression:

Stored size: 1004 Bytes

Contents

require_dependency 'spree/shipping_calculator'

module Spree
  module Calculator::Shipping
    class FlexiRate < ShippingCalculator
      preference :first_item,      :decimal, default: 0.0
      preference :additional_item, :decimal, default: 0.0
      preference :max_items,       :integer, default: 0
      preference :currency,        :string,  default: ->{ Spree::Config[:currency] }

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

      def compute_package(package)
        compute_from_quantity(package.contents.sum(&:quantity))
      end

      def compute_from_quantity(quantity)
        sum = 0
        max = preferred_max_items.to_i
        quantity.times do |i|
          # check max value to avoid divide by 0 errors
          if (max == 0 && i == 0) || (max > 0) && (i % max == 0)
            sum += preferred_first_item.to_f
          else
            sum += preferred_additional_item.to_f
          end
        end

        sum
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
solidus_core-2.0.3 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-1.4.2 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-1.3.2 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.0.2 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-1.4.1 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.0.1 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.0.0 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-1.4.0 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.0.0.rc1 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-1.4.0.rc1 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.0.0.beta1 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-1.4.0.beta1 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-1.3.1 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-1.3.0 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-1.3.0.rc2 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-1.3.0.rc1 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-1.3.0.beta1 app/models/spree/calculator/shipping/flexi_rate.rb