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
Exclude order's own reservations from stock availability checks (#14040)
Damian Legawieca5c8f11a242f
diff --git a/spree/core/app/models/spree/line_item.rb b/spree/core/app/models/spree/line_item.rbindex bdc64d6d1a..3640f913c6 100644--- a/spree/core/app/models/spree/line_item.rb+++ b/spree/core/app/models/spree/line_item.rb@@ -165,9 +165,13 @@ module Spree# Returns true if the line item has sufficient stock#+ # The order's own active stock reservations are excluded from the+ # availability check — a customer's own checkout hold must not make+ # their own line item look out of stock.+ ## @return [Boolean]def sufficient_stock?- can_supply? quantity+ Spree::Stock::Quantifier.new(variant, excluded_order: order).can_supply?(quantity)end# Returns true if the line item has insufficient stockdiff --git a/spree/core/app/models/spree/stock/availability_validator.rb b/spree/core/app/models/spree/stock/availability_validator.rbindex 2b70d2b026..b424b55b58 100644--- a/spree/core/app/models/spree/stock/availability_validator.rb+++ b/spree/core/app/models/spree/stock/availability_validator.rb@@ -28,7 +28,7 @@ module Spreeprivatedef item_available?(line_item, quantity)- Spree::Stock::Quantifier.new(line_item.variant).can_supply?(quantity)+ Spree::Stock::Quantifier.new(line_item.variant, excluded_order: line_item.order).can_supply?(quantity)endendenddiff --git a/spree/core/app/models/spree/stock/quantifier.rb b/spree/core/app/models/spree/stock/quantifier.rbindex 2af9363675..abe2e898aa 100644--- a/spree/core/app/models/spree/stock/quantifier.rb+++ b/spree/core/app/models/spree/stock/quantifier.rb@@ -1,11 +1,16 @@module Spreemodule Stockclass Quantifier- attr_reader :variant, :stock_location+ attr_reader :variant, :stock_location, :excluded_order- def initialize(variant, stock_location = nil)- @variant = variant- @stock_location = stock_location+ # @param excluded_order [Spree::Order, nil] when given, reservations+ # belonging to this order are not counted against availability. Used+ # when checking an order's own line items so the customer's own+ # checkout hold doesn't make their item look out of stock.+ def initialize(variant, stock_location = nil, excluded_order: nil)+ @variant = variant+ @stock_location = stock_location+ @excluded_order = excluded_orderend# Units a customer can purchase right now: physical pool minus@@ -53,16 +58,28 @@ module Spree# multi-location variant would subtract reservations from other# warehouses.#+ # When +excluded_order+ is set, that order's own reservations are left+ # out of the count so an order's own checkout hold doesn't count+ # against the availability of its own line items.+ ## @return [Integer]def reserved_quantityreturn @reserved_quantity if defined?(@reserved_quantity)return @reserved_quantity = 0 unless Spree::Config[:stock_reservations_enabled]return @reserved_quantity = 0 if stock_items.blank?+ excluded_order_id = excluded_order&.id+@reserved_quantity = if reservations_preloaded?- stock_items.sum { |si| si.active_stock_reservations.sum(&:quantity) }+ stock_items.sum do |si|+ reservations = si.active_stock_reservations+ reservations = reservations.reject { |r| r.order_id == excluded_order_id } if excluded_order_id+ reservations.sum(&:quantity)+ endelse- Spree::StockReservation.active.where(stock_item_id: stock_items.map(&:id)).sum(:quantity)+ reservations = Spree::StockReservation.active.where(stock_item_id: stock_items.map(&:id))+ reservations = reservations.where.not(order_id: excluded_order_id) if excluded_order_id+ reservations.sum(:quantity)endenddiff --git a/spree/core/app/services/spree/cart/remove_out_of_stock_items.rb b/spree/core/app/services/spree/cart/remove_out_of_stock_items.rbindex 258648e8eb..01cb21a573 100644--- a/spree/core/app/services/spree/cart/remove_out_of_stock_items.rb+++ b/spree/core/app/services/spree/cart/remove_out_of_stock_items.rb@@ -9,7 +9,7 @@ module Spreereturn success([order, @messages, @warnings]) if order.item_count.zero? || order.line_items.none?- line_items = order.line_items.includes(variant: [:product, :stock_items, :stock_locations, { stock_items: :stock_location }])+ line_items = order.line_items.includes(variant: [:product, :stock_locations, { stock_items: [:stock_location, :active_stock_reservations] }])ActiveRecord::Base.transaction doline_items.each do |line_item|diff --git a/spree/core/spec/models/spree/stock/quantifier_spec.rb b/spree/core/spec/models/spree/stock/quantifier_spec.rbindex 5445e05df3..2978ad8a25 100644--- a/spree/core/spec/models/spree/stock/quantifier_spec.rb+++ b/spree/core/spec/models/spree/stock/quantifier_spec.rb@@ -179,6 +179,62 @@ module Spree)expect(subject.reserved_quantity).to eq(3)end++ context 'with an excluded order' do+ subject { described_class.new(stock_item.variant, excluded_order: own_order) }++ let(:own_order) { create(:order) }+ let(:own_line_item) { create(:line_item, order: own_order, variant: stock_item.variant) }++ it "does not count the excluded order's own reservations" do+ create(+ :stock_reservation,+ stock_item: stock_item,+ line_item: own_line_item,+ order: own_order,+ quantity: 4,+ expires_at: 5.minutes.from_now+ )+ expect(subject.reserved_quantity).to eq(0)+ expect(subject.total_on_hand).to eq(stock_item.count_on_hand)+ end++ it "still counts other orders' reservations" do+ create(+ :stock_reservation,+ stock_item: stock_item,+ line_item: own_line_item,+ order: own_order,+ quantity: 4,+ expires_at: 5.minutes.from_now+ )+ create(+ :stock_reservation,+ stock_item: stock_item,+ line_item: other_line_item,+ order: other_order,+ quantity: 2,+ expires_at: 5.minutes.from_now+ )+ expect(subject.reserved_quantity).to eq(2)+ end++ context 'when the excluded order is not yet persisted' do+ subject { described_class.new(stock_item.variant, excluded_order: Spree::Order.new) }++ it 'counts all reservations rather than excluding everything' do+ create(+ :stock_reservation,+ stock_item: stock_item,+ line_item: other_line_item,+ order: other_order,+ quantity: 2,+ expires_at: 5.minutes.from_now+ )+ expect(subject.reserved_quantity).to eq(2)+ end+ end+ endendcontext 'when stock_reservations_enabled is false' dodiff --git a/spree/core/spec/services/spree/cart/remove_out_of_stock_items_spec.rb b/spree/core/spec/services/spree/cart/remove_out_of_stock_items_spec.rbindex ca31d2188b..c76395677f 100644--- a/spree/core/spec/services/spree/cart/remove_out_of_stock_items_spec.rb+++ b/spree/core/spec/services/spree/cart/remove_out_of_stock_items_spec.rb@@ -77,4 +77,33 @@ RSpec.describe Spree::Cart::RemoveOutOfStockItems doexpect(messages.to_sentence).to eq(Spree.t('cart_line_item.discontinued', li_name: variant.product.name))endend++ context "when the order holds its own stock reservation for the last unit" do+ let(:line_item) { order.line_items.first }++ before do+ Spree::Config[:stock_reservations_enabled] = true+ variant.stock_items.update_all(count_on_hand: 1, backorderable: false)+ line_item.update!(quantity: 1)+ variant.stock_items.each do |stock_item|+ create(+ :stock_reservation,+ stock_item: stock_item,+ line_item: line_item,+ order: order,+ quantity: 1,+ expires_at: 5.minutes.from_now+ )+ end+ end++ after { Spree::Config[:stock_reservations_enabled] = true }++ it 'keeps the line item in the cart' do+ _order, messages, warnings = execute.value+ expect(messages).to be_empty+ expect(warnings).to be_empty+ expect(order.reload.line_items).to include(line_item)+ end+ endend