Sha256: 094210c2cdc4beea1c1e431593429209c295944a8e4557b2f7560a251de68e79

Contents?: true

Size: 1.46 KB

Versions: 62

Compression:

Stored size: 1.46 KB

Contents

module Spree
  module Checkout
    class GetShippingRates
      prepend Spree::ServiceModule::Base

      def call(order:)
        run :reload_order
        run :ensure_shipping_address
        run :ensure_line_items_present
        run :generate_or_return_shipping_rates
      end

      private

      # we need to reload order to fetch the most up-to-date version of it
      def reload_order(order:)
        success(order: order.reload)
      end

      def ensure_shipping_address(order:)
        return failure([], Spree.t('errors.services.get_shipping_rates.no_shipping_address')) if order.ship_address.blank?

        success(order: order)
      end

      def ensure_line_items_present(order:)
        return failure([], Spree.t('errors.services.get_shipping_rates.no_line_items')) if order.line_items.empty?

        success(order: order)
      end

      def generate_or_return_shipping_rates(order:)
        generate_shipping_rates(order: order) if order.shipments.empty?
        return_shipments(order: order)
      end

      def generate_shipping_rates(order:)
        ApplicationRecord.transaction do
          order.create_proposed_shipments
          order.create_shipment_tax_charge!
          order.set_shipments_cost
          order.apply_free_shipping_promotions
        end
        success(order: order.reload)
      end

      def return_shipments(order:)
        success(order.reload.shipments.includes([shipping_rates: :shipping_method]))
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
spree_core-4.10.1 app/services/spree/checkout/get_shipping_rates.rb
spree_core-4.10.0 app/services/spree/checkout/get_shipping_rates.rb
spree_core-4.9.0 app/services/spree/checkout/get_shipping_rates.rb
spree_core-4.8.3 app/services/spree/checkout/get_shipping_rates.rb
spree_core-4.8.2 app/services/spree/checkout/get_shipping_rates.rb
spree_core-4.7.3 app/services/spree/checkout/get_shipping_rates.rb
spree_core-4.6.6 app/services/spree/checkout/get_shipping_rates.rb
spree_core-4.5.5 app/services/spree/checkout/get_shipping_rates.rb
spree_core-4.0.9 app/services/spree/checkout/get_shipping_rates.rb
spree_core-4.1.15 app/services/spree/checkout/get_shipping_rates.rb
spree_core-4.2.7 app/services/spree/checkout/get_shipping_rates.rb
spree_core-4.3.3 app/services/spree/checkout/get_shipping_rates.rb
spree_core-4.4.1 app/services/spree/checkout/get_shipping_rates.rb
spree_core-4.5.4 app/services/spree/checkout/get_shipping_rates.rb
spree_core-4.6.5 app/services/spree/checkout/get_shipping_rates.rb
spree_core-4.7.2 app/services/spree/checkout/get_shipping_rates.rb
spree_core-4.7.1 app/services/spree/checkout/get_shipping_rates.rb
spree_core-4.6.4 app/services/spree/checkout/get_shipping_rates.rb
spree_core-4.7.0 app/services/spree/checkout/get_shipping_rates.rb
spree_core-4.6.3 app/services/spree/checkout/get_shipping_rates.rb