Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Set order state to delivery if selected shipping rate is changed

Johann Boutetac08f8dfc153
diff --git a/core/app/services/spree/checkout/update.rb b/core/app/services/spree/checkout/update.rb
index 6fd272989e..4bc42439cf 100644
--- a/core/app/services/spree/checkout/update.rb
+++ b/core/app/services/spree/checkout/update.rb
@@ -8,7 +8,8 @@ module Spree
bill_changed = address_with_country_iso_present?(params, 'bill')
params = replace_country_iso_with_id(params, 'ship') if ship_changed
params = replace_country_iso_with_id(params, 'bill') if bill_changed
- order.state = 'address' if (ship_changed || bill_changed) && order.checkout_steps.include?('address')
+ order.state = 'address' if (ship_changed || bill_changed) && order.has_checkout_step?('address')
+ order.state = 'delivery' if selected_shipping_rate_present?(params) && order.has_checkout_step?('delivery')
return success(order) if order.update_from_params(params, permitted_attributes, request_env)
failure(order)
@@ -23,6 +24,13 @@ module Spree
true
end
+ def selected_shipping_rate_present?(params)
+ shipments_attributes = params.dig(:order, :shipments_attributes)
+ return false unless shipments_attributes
+
+ shipments_attributes.any? { |s| s.dig(:selected_shipping_rate_id) }
+ 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
diff --git a/core/spec/services/spree/checkout/update_spec.rb b/core/spec/services/spree/checkout/update_spec.rb
index a3661ac2f3..264e5d1194 100644
--- a/core/spec/services/spree/checkout/update_spec.rb
+++ b/core/spec/services/spree/checkout/update_spec.rb
@@ -102,4 +102,34 @@ describe Spree::Checkout::Update, type: :service do
expect(order.ship_address.state.id).to eq state.id
end
end
+
+ describe 'update selected shipping rate' do
+ let(:update_service) { described_class.new }
+ let(:order) { create(:order_with_line_items) }
+ let(:order_params) do
+ ActionController::Parameters.new(
+ order: {
+ shipments_attributes: [
+ {
+ id: order.shipments.first.id,
+ selected_shipping_rate_id: order.shipments.first.shipping_rates.first.id
+ }
+ ]
+ }
+ )
+ end
+ let(:permitted_attributes) do
+ Spree::PermittedAttributes.checkout_attributes + [
+ shipments_attributes: Spree::PermittedAttributes.shipment_attributes
+ ]
+ end
+
+ it 'should set order back to delivery state' do
+ expect(order.state).not_to eq 'delivery'
+
+ update_service.send(:call, order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
+
+ expect(order.state).to eq 'delivery'
+ end
+ end
end