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 N+1s and improve performance of `RemoveOutOfStockItems` service

Damian Legawiec3eedb76eceb1
diff --git a/api/lib/spree/api/testing_support/v2/current_order.rb b/api/lib/spree/api/testing_support/v2/current_order.rb
index 22f103595a..f0af23131a 100644
--- a/api/lib/spree/api/testing_support/v2/current_order.rb
+++ b/api/lib/spree/api/testing_support/v2/current_order.rb
@@ -1,6 +1,5 @@
def ensure_order_totals
- order.update_totals
- order.persist_totals
+ order.update_with_updater!
end
shared_context 'creates order with line item' do
diff --git a/core/app/models/spree/line_item.rb b/core/app/models/spree/line_item.rb
index bac7102f1d..50c7d8701a 100644
--- a/core/app/models/spree/line_item.rb
+++ b/core/app/models/spree/line_item.rb
@@ -50,7 +50,7 @@ module Spree
delegate :name, :description, :sku, :should_track_inventory?, :product, :options_text, :slug, :product_id, :dimensions_unit, :weight_unit, to: :variant
delegate :brand, :category, to: :product
delegate :tax_zone, to: :order
- delegate :digital?, to: :variant
+ delegate :digital?, :can_supply?, to: :variant
scope :with_digital_assets, -> { joins(:variant).merge(Spree::Variant.with_digital_assets) }
@@ -134,7 +134,7 @@ module Spree
alias money display_total
def sufficient_stock?
- Spree::Stock::Quantifier.new(variant).can_supply? quantity
+ can_supply? quantity
end
def insufficient_stock?
diff --git a/core/app/models/spree/stock/quantifier.rb b/core/app/models/spree/stock/quantifier.rb
index 29e0ff28e2..a4b3df4239 100644
--- a/core/app/models/spree/stock/quantifier.rb
+++ b/core/app/models/spree/stock/quantifier.rb
@@ -25,7 +25,7 @@ module Spree
end
def can_supply?(required = 1)
- variant.available? && (total_on_hand >= required || backorderable?)
+ variant.available? && (backorderable? || total_on_hand >= required)
end
def stock_items
diff --git a/core/app/services/spree/cart/remove_out_of_stock_items.rb b/core/app/services/spree/cart/remove_out_of_stock_items.rb
index 6b68a256d3..4f5c35d5bd 100644
--- a/core/app/services/spree/cart/remove_out_of_stock_items.rb
+++ b/core/app/services/spree/cart/remove_out_of_stock_items.rb
@@ -5,8 +5,13 @@ module Spree
def call(order:)
@messages = []
+
+ return success([order, @messages]) 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 }])
+
ActiveRecord::Base.transaction do
- line_items(order).each do |line_item|
+ line_items.each do |line_item|
cart_remove_line_item_service.call(order: order, line_item: line_item) if !valid_status?(line_item) || !stock_available?(line_item)
end
end
@@ -20,17 +25,6 @@ module Spree
private
- def line_items(order)
- if order.line_items.empty?
- []
- elsif order.line_items.first.association_cached?(:variant) && order.line_items.first.variant.association_cached?(:product)
- # Don't include associations if it is already included, because it breaks other includes
- order.line_items
- else
- order.line_items.includes(variant: :product)
- end
- end
-
def valid_status?(line_item)
product = line_item.product
if !product.active? || product.deleted? || product.discontinued? || line_item.variant.discontinued?