Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix: cancelling an edited order releases its stock (#14728)

Damian Legawiec886e379f23bd
diff --git a/spree/core/app/services/spree/orders/build_fulfillments.rb b/spree/core/app/services/spree/orders/build_fulfillments.rb
index 62e2dcc494..518e71eade 100644
--- a/spree/core/app/services/spree/orders/build_fulfillments.rb
+++ b/spree/core/app/services/spree/orders/build_fulfillments.rb
@@ -9,10 +9,16 @@ module Spree
#
# No-op when the order has no shipping address, no line items, or does
# not require delivery (digital orders, etc.).
+ #
+ # Also a no-op once the order is placed. Its fulfillments hold the stock
+ # promised at placement, and an edit adjusts them in place through
+ # Spree::OrderInventory. Rebuilding would delete them and leave that
+ # promise on rows nothing can cancel or ship, so the stock stays held.
class BuildFulfillments
prepend Spree::ServiceModule::Base
def call(order:)
+ return success(order) if order.completed?
return success(order) unless order.ship_address_id.present?
return success(order) unless order.line_items.any?
return success(order) unless order.delivery_step_required?
diff --git a/spree/core/spec/services/spree/orders/update_spec.rb b/spree/core/spec/services/spree/orders/update_spec.rb
index be870e158b..feda0b0390 100644
--- a/spree/core/spec/services/spree/orders/update_spec.rb
+++ b/spree/core/spec/services/spree/orders/update_spec.rb
@@ -290,6 +290,24 @@ module Spree
expect(order.customer_note).to eq('whatever')
end
end
+
+ # A placed order's fulfillments carry the stock it promised, so an
+ # edit adjusts them in place instead of replacing them.
+ context 'when the order is placed and items change' do
+ let(:params) { { items: [{ variant_id: variant.prefixed_id, quantity: 3 }] } }
+
+ before { order.update_columns(status: 'placed', completed_at: Time.current) }
+
+ it 'keeps the fulfillments and puts the new units on them' do
+ old_shipment_ids = order.shipments.map(&:id)
+
+ expect(subject).to be_success
+
+ order.reload
+ expect(order.shipments.map(&:id)).to match_array(old_shipment_ids)
+ expect(order.fulfillments.first.fulfillment_items.sum(:quantity)).to eq(3)
+ end
+ end
end
describe 'final totals refresh' do
diff --git a/spree/core/spec/workflows/spree/orders/cancel_spec.rb b/spree/core/spec/workflows/spree/orders/cancel_spec.rb
index 6548e7a8bc..b03cb9a5f6 100644
--- a/spree/core/spec/workflows/spree/orders/cancel_spec.rb
+++ b/spree/core/spec/workflows/spree/orders/cancel_spec.rb
@@ -45,6 +45,20 @@ module Spree
expect(stock_level.reload.count_on_hand).to eq(count_on_hand_before)
end
+ it 'withdraws what an edit after placement added to the promise' do
+ fulfillment = order.fulfillments.first
+ line_item = order.line_items.first
+ variant = line_item.variant
+ placed_quantity = line_item.quantity
+ stock_level = fulfillment.stock_location.stock_level(variant)
+ fulfillment.stock_location.allocate(variant, placed_quantity, fulfillment)
+
+ Spree::Orders::Update.call(order: order, params: { items: [{ variant_id: variant.prefixed_id, quantity: placed_quantity + 1 }] })
+
+ expect { subject.call(order: order.reload, canceler: user) }.
+ to change { stock_level.reload.allocated_count }.by(-(placed_quantity + 1))
+ end
+
# The carrier calls are batched after the order transaction commits, so
# they must still happen — just not from inside the fulfillment workflow.
it 'tells each provider to stand down' do