Sha256: 52c2275aae7472d6912ae6f18df598aee0410089f40f8825d3fdff6301e234ae

Contents?: true

Size: 813 Bytes

Versions: 3

Compression:

Stored size: 813 Bytes

Contents

module Spree
  class ShippingMatrixCalculator < Spree::ShippingCalculator
    preference :matrix_id, :matrix_id

    def self.description
      'Shipping Matrix Calculator'
    end

    def compute_package(package)
      matched_amount(user: package.contents.first.inventory_unit.order.user,
                     line_item_total: total(package.contents))
    end

    private

    def matched_amount(info)
      matched = matched_rule(info)

      if matched.nil?
        0
      else
        matched.amount
      end
    end

    def matched_rule(info)
      matched = matrix.rules.find { |rule| rule.matches?(info) }

      if matched.nil?
        matrix.rules.last
      else
        matched
      end
    end

    def matrix
      @matrix ||= Spree::ShippingMatrix.find(preferred_matrix_id)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spree_shipping_matrix-0.2.1 app/models/spree/shipping_matrix_calculator.rb
spree_shipping_matrix-0.2.0 app/models/spree/shipping_matrix_calculator.rb
spree_shipping_matrix-0.1.0 app/models/spree/shipping_matrix_calculator.rb