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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Never link cart-phase fulfillment items to an order
Damian Legawiecef5dfb423e3c
diff --git a/spree/core/app/models/spree/fulfillment.rb b/spree/core/app/models/spree/fulfillment.rbindex 5f48b293ee..b8bd564718 100644--- a/spree/core/app/models/spree/fulfillment.rb+++ b/spree/core/app/models/spree/fulfillment.rb@@ -532,13 +532,18 @@ module Spreeowner.set_fulfillments_costend- def set_up_inventory(status, variant, order, line_item, quantity = 1)+ # +owner+ is the cart or order the units belong to. The item's order_id is+ # a completion-time denormalization (backorder processing joins through+ # it): during checkout it stays nil — writing a cart's id into an order+ # foreign key would silently link the item to whatever order shares that+ # id. Carts::Complete stamps the real order onto the copied items.+ def set_up_inventory(status, variant, owner, line_item, quantity = 1)return if quantity <= 0fulfillment_items.create(status: status,variant_id: variant.id,- order_id: order.id,+ order_id: owner.is_a?(Spree::Order) ? owner.id : nil,line_item_id: line_item.id,quantity: quantity)diff --git a/spree/core/app/models/spree/fulfillment_item.rb b/spree/core/app/models/spree/fulfillment_item.rbindex 84a753cb77..2502ff6315 100644--- a/spree/core/app/models/spree/fulfillment_item.rb+++ b/spree/core/app/models/spree/fulfillment_item.rb@@ -6,7 +6,9 @@ module Spreewith_options inverse_of: :fulfillment_items dobelongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant'- belongs_to :order, class_name: 'Spree::Order'+ # Optional because cart-phase items have no order yet — their owner is+ # reached through the fulfillment; order_id arrives at completion.+ belongs_to :order, class_name: 'Spree::Order', optional: truebelongs_to :fulfillment, class_name: 'Spree::Fulfillment', touch: true, optional: truehas_many :return_line_items, class_name: 'Spree::ReturnLineItem', inverse_of: :fulfillment_itemhas_many :returns, class_name: 'Spree::Return', through: :return_line_itemsdiff --git a/spree/core/lib/tasks/carts_migration.rake b/spree/core/lib/tasks/carts_migration.rakeindex 5b3e982ee0..557894df29 100644--- a/spree/core/lib/tasks/carts_migration.rake+++ b/spree/core/lib/tasks/carts_migration.rake@@ -10,6 +10,19 @@# Orders holding payment sessions convert last so an interrupted run leaves# the riskiest rows for the retry. Idempotent — converted orders are gone.namespace :spree do+ desc 'Detach cart-phase fulfillment items from orders they never belonged to'+ # Early 6.0 builds wrote the cart's id into fulfillment_items.order_id during+ # checkout, silently linking the item to whichever order shared that id.+ # Cart-phase items carry no order — completion stamps the real one onto the+ # copied rows. Idempotent.+ task repair_cart_fulfillment_item_orders: :environment do+ count = Spree::FulfillmentItem.+ where.not(order_id: nil).+ where(fulfillment_id: Spree::Fulfillment.where.not(cart_id: nil).select(:id)).+ update_all(order_id: nil)+ puts "spree_fulfillment_items: #{count} cart-phase rows detached from orders"+ end+desc 'Convert incomplete legacy orders into Spree::Cart rows'task migrate_incomplete_orders_to_carts: :environment dobatch_size = ENV.fetch('BATCH_SIZE', 250).to_idiff --git a/spree/core/spec/models/spree/shipment_spec.rb b/spree/core/spec/models/spree/shipment_spec.rbindex 73343d407a..3d21ccd280 100644--- a/spree/core/spec/models/spree/shipment_spec.rb+++ b/spree/core/spec/models/spree/shipment_spec.rb@@ -1065,6 +1065,17 @@ describe Spree::Shipment, type: :model doexpect(inventory_units).to receive(:create).with(params)shipment.set_up_inventory('on_hand', variant, order, line_item)end++ # A cart's id written into order_id dereferences as whatever ORDER shares+ # that id — during checkout the item silently linked itself to a stranger's+ # order, whose ship address then stood in for the customer's at rating,+ # and whose backorder queue could swallow the cart's units.+ it 'leaves order_id nil when the owner is a cart' do+ cart = create(:cart, store: @default_store)++ expect(inventory_units).to receive(:create).with(params.merge(order_id: nil))+ shipment.set_up_inventory('on_hand', variant, cart, line_item)+ endend# Regression test for #3349