From b24cb5e5e753e2fa911a1db7d7b1b355617582d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Busqu=C3=A9?= Date: Fri, 11 Aug 2023 11:24:25 +0200 Subject: [PATCH] Fix error added when resetting order flow on an empty order https://github.com/solidusio/solidus/pull/4369 introduced a regression where when calling `Spree::Order#restart_checkout_flow` on an empty order, an error would be added to the order. That happened when calling `#next` and the validation failed because no line items were present. We restore the previous behavior where we only try going to the `"address"` state if the order has line items. (cherry picked from commit 82c7bdf73e29ea1e5fb2b402786f6e434dda422f) --- core/app/models/spree/order.rb | 2 +- core/spec/models/spree/order_spec.rb | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb index 644242c141d..8364e269f9d 100644 --- a/core/app/models/spree/order.rb +++ b/core/app/models/spree/order.rb @@ -522,7 +522,7 @@ def restart_checkout_flow state: 'cart', updated_at: Time.current ) - self.next + self.next if line_items.any? end def refresh_shipment_rates diff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rb index 2c81f958d6f..8511d250eb3 100644 --- a/core/spec/models/spree/order_spec.rb +++ b/core/spec/models/spree/order_spec.rb @@ -579,10 +579,17 @@ def merge!(other_order, user = nil) end context "without line items" do + let(:order) { create(:order, state: "delivery", line_items: []) } + it "updates the state column to cart" do - order = create(:order, state: "delivery") expect{ order.restart_checkout_flow }.to change{ order.state }.from("delivery").to("cart") end + + it "doesn't add errors to the order" do + order.restart_checkout_flow + + expect(order.errors).to be_empty + end end end