Sha256: 015ac89d4c966520894876ce233acd7c8fcea437862f763e2215a7053a025e43

Contents?: true

Size: 1.22 KB

Versions: 42

Compression:

Stored size: 1.22 KB

Contents

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

      def call(order:, params:, permitted_attributes:, request_env:)
        params = replace_country_iso_with_id(params, 'ship') if address_with_country_iso_present?(params, 'ship')
        params = replace_country_iso_with_id(params, 'bill') if address_with_country_iso_present?(params, 'bill')
        return success(order) if order.update_from_params(params, permitted_attributes, request_env)

        failure(order)
      end

      private

      def address_with_country_iso_present?(params, address_kind = 'ship')
        return false unless params.dig(:order, "#{address_kind}_address_attributes".to_sym, :country_iso)
        return false if params.dig(:order, "#{address_kind}_address_attributes".to_sym, :country_id)

        true
      end

      def replace_country_iso_with_id(params, address_kind = 'ship')
        country_id = Spree::Country.by_iso(params[:order]["#{address_kind}_address_attributes"].fetch(:country_iso))&.id

        params[:order]["#{address_kind}_address_attributes"]['country_id'] = country_id
        params[:order]["#{address_kind}_address_attributes"].delete(:country_iso)
        params
      end
    end
  end
end

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
spree_core-3.7.14.1 app/services/spree/checkout/update.rb
spree_core-3.7.14 app/services/spree/checkout/update.rb
spree_core-3.7.13 app/services/spree/checkout/update.rb
spree_core-4.0.5 app/services/spree/checkout/update.rb
spree_core-3.7.12 app/services/spree/checkout/update.rb
spree_core-4.0.4 app/services/spree/checkout/update.rb
spree_core-3.7.11 app/services/spree/checkout/update.rb
spree_core-4.2.0.beta app/services/spree/checkout/update.rb
spree_core-4.1.9 app/services/spree/checkout/update.rb
spree_core-4.1.8 app/services/spree/checkout/update.rb
spree_core-4.1.7 app/services/spree/checkout/update.rb
spree_core-4.1.6 app/services/spree/checkout/update.rb
spree_core-4.1.5 app/services/spree/checkout/update.rb
spree_core-4.1.4 app/services/spree/checkout/update.rb
spree_core-4.1.3 app/services/spree/checkout/update.rb
spree_core-4.1.2 app/services/spree/checkout/update.rb
spree_core-4.1.1 app/services/spree/checkout/update.rb
spree_core-4.1.0 app/services/spree/checkout/update.rb
spree_core-4.1.0.rc3 app/services/spree/checkout/update.rb
spree_core-4.1.0.rc2 app/services/spree/checkout/update.rb