Sha256: ccd08bf3e3c80626fbef0dcee4107d78d281e1e5e3f99a9bc2f899cf2b099f0a

Contents?: true

Size: 994 Bytes

Versions: 43

Compression:

Stored size: 994 Bytes

Contents

# frozen_string_literal: true

require_dependency 'spree/calculator'
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 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

43 entries across 43 versions & 2 rubygems

Version Path
solidus_core-2.9.6 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.8.6 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.9.5 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.9.4 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.6.6 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.7.4 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.8.5 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.9.3 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.9.2 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.10.0.beta1 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.7.3 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.6.5 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.9.1 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.9.0 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.9.0.rc.1 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.8.4 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.8.3 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.7.2 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.6.4 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.8.2 app/models/spree/calculator/shipping/flexi_rate.rb