Sha256: 7386a538749c0766ef8c2aa0ec83dd4f635b94d07e3396568f76330dd00a4777

Contents?: true

Size: 1.1 KB

Versions: 10

Compression:

Stored size: 1.1 KB

Contents

module Spree
  module Cart
    class EstimateShippingRates
      prepend Spree::ServiceModule::Base

      def call(order:, country_iso: nil)
        country_id = country_id(country_iso)
        dummy_order = generate_dummy_order(order, country_id)

        packages = ::Spree::Stock::Coordinator.new(dummy_order).packages
        estimator = ::Spree::Stock::Estimator.new(dummy_order)
        shipping_rates = if order.line_items.any? && packages.any?
                           estimator.shipping_rates(packages.first)
                         else
                           []
                         end

        success(shipping_rates)
      end

      private

      def country_id(country_iso)
        if country_iso.present?
          ::Spree::Country.by_iso(country_iso)&.id
        else
          Spree::Config[:default_country_id]
        end
      end

      def generate_dummy_order(order, country_id)
        dummy_order = order.dup
        dummy_order.line_items = order.line_items
        dummy_order.ship_address = ::Spree::Address.new(country_id: country_id)
        dummy_order
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
spree_core-4.2.7 app/services/spree/cart/estimate_shipping_rates.rb
spree_core-4.2.6 app/services/spree/cart/estimate_shipping_rates.rb
spree_core-4.2.5 app/services/spree/cart/estimate_shipping_rates.rb
spree_core-4.2.4 app/services/spree/cart/estimate_shipping_rates.rb
spree_core-4.2.3.1 app/services/spree/cart/estimate_shipping_rates.rb
spree_core-4.2.3 app/services/spree/cart/estimate_shipping_rates.rb
spree_core-4.2.2 app/services/spree/cart/estimate_shipping_rates.rb
spree_core-4.2.1 app/services/spree/cart/estimate_shipping_rates.rb
spree_core-4.2.0 app/services/spree/cart/estimate_shipping_rates.rb
spree_core-4.2.0.rc5 app/services/spree/cart/estimate_shipping_rates.rb