mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
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.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user