Sha256: b57447be42f527f2e48c8a11e7e0ff1afe446d8890edcb953ea827567862d18f
Contents?: true
Size: 1.79 KB
Versions: 9
Compression:
Stored size: 1.79 KB
Contents
module Spree module Stock class Estimator attr_reader :order, :currency def initialize(order) @order = order @currency = order.currency end def shipping_rates(package, frontend_only = true) rates = calculate_shipping_rates(package) rates.select! { |rate| rate.shipping_method.frontend? } if frontend_only choose_default_shipping_rate(rates) sort_shipping_rates(rates) end private def choose_default_shipping_rate(shipping_rates) unless shipping_rates.empty? shipping_rates.min_by(&:cost).selected = true end end def sort_shipping_rates(shipping_rates) shipping_rates.sort_by!(&:cost) end def calculate_shipping_rates(package) shipping_methods(package).map do |shipping_method| cost = shipping_method.calculator.compute(package) shipping_method.shipping_rates.new(cost: cost) if cost end.compact end def shipping_methods(package) package.shipping_methods.select do |ship_method| calculator = ship_method.calculator begin calculator.available?(package) && ship_method.include?(order.ship_address) && (calculator.preferences[:currency].nil? || calculator.preferences[:currency] == currency) rescue Exception => exception log_calculator_exception(ship_method, exception) end end end def log_calculator_exception(ship_method, exception) Rails.logger.info("Something went wrong calculating rates with the #{ship_method.name} (ID=#{ship_method.id}) shipping method.") Rails.logger.info("*" * 50) Rails.logger.info(exception.backtrace.join("\n")) end end end end
Version data entries
9 entries across 9 versions & 1 rubygems