From 4a6e4d4c6d70959425f8ba8ac812b6a238c7f854 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Wed, 30 Oct 2024 15:53:15 +1100 Subject: [PATCH 1/2] Ensure shipment is updated when using `update_or_create` `Spree::OrderContents#update_or_create` is used to update the cart when on the /shop page. If you start an order and proceed to the "Order summary" step, and then decide to update your order by using the shop link next to the cart, such update wouldn't update the shipment. This result in the order page in the backoffice displaying the wrong data, and more importantly, in the stock not being updated. So now we ensure shipment will be updated, which result in the checkout flow being restarted, thus making sure the shipment is updated. --- app/models/spree/order_contents.rb | 1 + spec/models/spree/order_contents_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/app/models/spree/order_contents.rb b/app/models/spree/order_contents.rb index 6c8e150e02..e295d3cc6c 100644 --- a/app/models/spree/order_contents.rb +++ b/app/models/spree/order_contents.rb @@ -38,6 +38,7 @@ module Spree line_item.price = variant.price order.line_items << line_item end + update_shipment order.reload line_item diff --git a/spec/models/spree/order_contents_spec.rb b/spec/models/spree/order_contents_spec.rb index b31d685ea3..9f7ec83221 100644 --- a/spec/models/spree/order_contents_spec.rb +++ b/spec/models/spree/order_contents_spec.rb @@ -166,6 +166,12 @@ RSpec.describe Spree::OrderContents do end describe "#update_or_create" do + it "ensures shipments are updated" do + expect(order).to receive(:ensure_updated_shipments) + + subject.update_or_create(variant, { quantity: 2, max_quantity: 3 }) + end + describe "creating" do it "creates a new line item with given attributes" do subject.update_or_create(variant, { quantity: 2, max_quantity: 3 }) From cedf040b478e161fe7c2ce0dcf3397c8e5d73861 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Wed, 4 Dec 2024 22:15:56 +1100 Subject: [PATCH 2/2] Per review, test on create and update --- spec/models/spree/order_contents_spec.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/spec/models/spree/order_contents_spec.rb b/spec/models/spree/order_contents_spec.rb index 9f7ec83221..d3a55fcf8b 100644 --- a/spec/models/spree/order_contents_spec.rb +++ b/spec/models/spree/order_contents_spec.rb @@ -166,12 +166,6 @@ RSpec.describe Spree::OrderContents do end describe "#update_or_create" do - it "ensures shipments are updated" do - expect(order).to receive(:ensure_updated_shipments) - - subject.update_or_create(variant, { quantity: 2, max_quantity: 3 }) - end - describe "creating" do it "creates a new line item with given attributes" do subject.update_or_create(variant, { quantity: 2, max_quantity: 3 }) @@ -181,6 +175,12 @@ RSpec.describe Spree::OrderContents do expect(line_item.max_quantity).to eq 3 expect(line_item.price).to eq variant.price end + + it "ensures shipments are updated" do + expect(order).to receive(:ensure_updated_shipments) + + subject.update_or_create(variant, { quantity: 2, max_quantity: 3 }) + end end describe "updating" do @@ -192,6 +192,12 @@ RSpec.describe Spree::OrderContents do expect(line_item.reload.quantity).to eq 3 expect(line_item.max_quantity).to eq 4 end + + it "ensures shipments are updated" do + expect(order).to receive(:ensure_updated_shipments) + + subject.update_or_create(variant, { quantity: 3, max_quantity: 4 }) + end end end end