diff --git a/app/controllers/concerns/checkout_steps.rb b/app/controllers/concerns/checkout_steps.rb index 556c934166..b9480998f7 100644 --- a/app/controllers/concerns/checkout_steps.rb +++ b/app/controllers/concerns/checkout_steps.rb @@ -40,6 +40,10 @@ module CheckoutSteps redirect_to_step_based_on_order end + # Checkout step and allowed order state + # * step details : order state in cart, address or delivery + # * step payment : order state is payment + # * step summary : order state is confirmation def check_step case @order.state when "cart", "address", "delivery" @@ -48,14 +52,4 @@ module CheckoutSteps redirect_to checkout_step_path(:payment) if summary_step? end end - - def update_order_state - if @order.state == "confirmation" && payment_step? - @order.back_to_payment - end - - return unless @order.state.in?(["payment", "confirmation"]) && details_step? - - @order.back_to_address - end end diff --git a/app/controllers/split_checkout_controller.rb b/app/controllers/split_checkout_controller.rb index 67d52c496a..ed41eb1609 100644 --- a/app/controllers/split_checkout_controller.rb +++ b/app/controllers/split_checkout_controller.rb @@ -23,10 +23,12 @@ class SplitCheckoutController < ::BaseController before_action :hide_ofn_navigation, only: [:edit, :update] def edit - redirect_to_step_based_on_order unless params[:step] - - update_order_state if params[:step] - check_step if params[:step] + if params[:step].blank? + redirect_to_step_based_on_order + else + update_order_state + check_step + end return if available_shipping_methods.any? @@ -128,4 +130,15 @@ class SplitCheckoutController < ::BaseController def order_params @order_params ||= Checkout::Params.new(@order, params, spree_current_user).call end + + # Update order state based on the step we are loading to avoid discrepancy between step and order + # state. We need to do this when moving back to a previous checkout step, the update action takes + # care of moving the order state forward. + def update_order_state + return @order.back_to_payment if @order.confirmation? && payment_step? + + return unless @order.after_delivery_state? && details_step? + + @order.back_to_address + end end diff --git a/app/models/spree/order.rb b/app/models/spree/order.rb index 2a3c825e44..9e41af5d5c 100644 --- a/app/models/spree/order.rb +++ b/app/models/spree/order.rb @@ -616,6 +616,10 @@ module Spree state.in?(["cart", "address", "delivery"]) end + def after_delivery_state? + state.in?(["payment", "confirmation"]) + end + private def reapply_tax_on_changed_address diff --git a/spec/controllers/split_checkout_controller_spec.rb b/spec/controllers/split_checkout_controller_spec.rb index 2d45f1b661..c6c24e52b7 100644 --- a/spec/controllers/split_checkout_controller_spec.rb +++ b/spec/controllers/split_checkout_controller_spec.rb @@ -74,6 +74,7 @@ describe SplitCheckoutController, type: :controller do it "updates the order state to payment" do get :edit, params: { step: "payment" } + expect(response.status).to eq 200 expect(order.reload.state).to eq("payment") end end @@ -82,6 +83,7 @@ describe SplitCheckoutController, type: :controller do it "updates the order state to address" do get :edit, params: { step: "details" } + expect(response.status).to eq 200 expect(order.reload.state).to eq("address") end end @@ -96,6 +98,7 @@ describe SplitCheckoutController, type: :controller do it "updates the order state to address" do get :edit, params: { step: "details" } + expect(response.status).to eq 200 expect(order.reload.state).to eq("address") end end