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.

Improve performance of `LineItems::FindByVariant` finder (#12101)

Damian Legawieca85b8cf083c0
diff --git a/core/app/finders/spree/line_items/find_by_variant.rb b/core/app/finders/spree/line_items/find_by_variant.rb
index 8e3910ade6..bb8d4cef0d 100644
--- a/core/app/finders/spree/line_items/find_by_variant.rb
+++ b/core/app/finders/spree/line_items/find_by_variant.rb
@@ -2,11 +2,13 @@ module Spree
module LineItems
class FindByVariant
def execute(order:, variant:, options: {})
- order.line_items.detect do |line_item|
- next unless line_item.variant_id == variant.id
+ line_item = order.line_items.loaded? ? order.line_items.detect { |li| li.variant_id == variant.id } : order.line_items.find_by(variant_id: variant.id)
+ if line_item
Spree::Dependencies.cart_compare_line_items_service.constantize.call(order: order, line_item: line_item, options: options).value
end
+
+ line_item
end
end
end