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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex 6fd272989e..4bc42439cf 100644--- a/core/app/services/spree/checkout/update.rb+++ b/core/app/services/spree/checkout/update.rb@@ -8,7 +8,8 @@ module Spreebill_changed = address_with_country_iso_present?(params, 'bill')params = replace_country_iso_with_id(params, 'ship') if ship_changedparams = 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 Spreetrueend+ 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))&.iddiff --git a/core/spec/services/spree/checkout/update_spec.rb b/core/spec/services/spree/checkout/update_spec.rbindex 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 doexpect(order.ship_address.state.id).to eq state.idendend++ 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+ endend