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.8.1 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.6.3 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.8.0 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.7.1 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.6.2 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core_devise_token_auth-2.8.0.alpha.14 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core_devise_token_auth-2.8.0.alpha.13 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core_devise_token_auth-2.8.0.alpha.12 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core_devise_token_auth-2.8.0.alpha.11 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core_devise_token_auth-2.8.0.alpha.10 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core_devise_token_auth-2.8.0.alpha.9 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core_devise_token_auth-2.8.0.alpha.8 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core_devise_token_auth-2.8.0.alpha.7 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core_devise_token_auth-2.8.0.alpha.6 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core_devise_token_auth-2.8.0.alpha.5 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core_devise_token_auth-2.8.0.alpha.4 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core_devise_token_auth-2.8.0.alpha.3 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core_devise_token_auth-2.8.0.alpha.1 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core_devise_token_auth-2.8.0.alpha.0 app/models/spree/calculator/shipping/flexi_rate.rb
solidus_core-2.7.0 app/models/spree/calculator/shipping/flexi_rate.rb