From 1fcbf6b44dcc0981d287ffc412c968f1eed4f8d3 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Fri, 13 Oct 2017 12:45:02 +1100 Subject: [PATCH] Clear shipping_method_id from order when restarting checkout If the order is allowed to retain a shipping_method_id, then subsequent saves of the order will cause a new shipment to be initialised. Seems to only happen for delivery shipping methods. This is undesirable because fees for the new shipment will appear in the checkout summary, which is not smart enough to recognise existing shipment fees and adjust the order total accordingly. --- app/controllers/checkout_controller.rb | 1 + spec/controllers/checkout_controller_spec.rb | 3 +++ 2 files changed, 4 insertions(+) diff --git a/app/controllers/checkout_controller.rb b/app/controllers/checkout_controller.rb index 817be578b0..ee0bb1f77d 100644 --- a/app/controllers/checkout_controller.rb +++ b/app/controllers/checkout_controller.rb @@ -163,6 +163,7 @@ class CheckoutController < Spree::CheckoutController def restart_checkout return if @order.state == 'cart' @order.restart_checkout! # resets state to 'cart' + @order.update_attributes!(shipping_method_id: nil) @order.shipments.with_state(:pending).destroy_all @order.payments.with_state(:checkout).destroy_all @order.reload diff --git a/spec/controllers/checkout_controller_spec.rb b/spec/controllers/checkout_controller_spec.rb index 51351797b1..70332b5ea5 100644 --- a/spec/controllers/checkout_controller_spec.rb +++ b/spec/controllers/checkout_controller_spec.rb @@ -223,6 +223,7 @@ describe CheckoutController do let!(:payment_failed) { create(:payment, order: order, state: 'failed') } before do + order.update_attribute(:shipping_method_id, shipment_pending.shipping_method_id) controller.instance_variable_set(:@order, order.reload) end @@ -242,12 +243,14 @@ describe CheckoutController do # 'pending' when the order has not been completed, so this is not a case that requires testing. it "resets the order state, and clears incomplete shipments and payments" do expect(order).to receive(:restart_checkout!).and_call_original + expect(order.shipping_method_id).to_not be nil expect(order.shipments.count).to be 1 expect(order.adjustments.shipping.count).to be 1 expect(order.payments.count).to be 2 expect(order.adjustments.payment_fee.count).to be 2 controller.send(:restart_checkout) expect(order.reload.state).to eq 'cart' + expect(order.shipping_method_id).to be nil expect(order.shipments.count).to be 0 expect(order.adjustments.shipping.count).to be 0 expect(order.payments.count).to be 1