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.

Minimize N+1s for fetching Cart/Orders in Storefront API (#12137)

Damian Legawieca4201f0aeb75
diff --git a/core/app/finders/spree/orders/find_complete.rb b/core/app/finders/spree/orders/find_complete.rb
index 3e1f39787f..e4347efec4 100644
--- a/core/app/finders/spree/orders/find_complete.rb
+++ b/core/app/finders/spree/orders/find_complete.rb
@@ -1,6 +1,8 @@
module Spree
module Orders
class FindComplete
+ include Spree::Orders::FinderHelper
+
attr_reader :user, :number, :token, :store
def initialize(user: nil, number: nil, token: nil, store: nil)
@@ -22,7 +24,7 @@ module Spree
private
def scope
- user? ? user.orders.complete.includes(scope_includes) : Spree::Order.complete.includes(scope_includes)
+ user? ? user.orders.complete.includes(order_includes) : Spree::Order.complete.includes(order_includes)
end
def user?
@@ -64,18 +66,6 @@ module Spree
orders.where(store: store)
end
-
- def scope_includes
- {
- line_items: [
- variant: [
- :images,
- option_values: :option_type,
- product: :product_properties,
- ]
- ]
- }
- end
end
end
end
diff --git a/core/app/finders/spree/orders/find_current.rb b/core/app/finders/spree/orders/find_current.rb
index 376723f977..0e06ce2e58 100644
--- a/core/app/finders/spree/orders/find_current.rb
+++ b/core/app/finders/spree/orders/find_current.rb
@@ -1,6 +1,8 @@
module Spree
module Orders
class FindCurrent
+ include Spree::Orders::FinderHelper
+
def execute(user:, store:, **params)
params = params.merge(store_id: store.id)
@@ -15,7 +17,7 @@ module Spree
private
def incomplete_orders
- Spree::Order.incomplete.not_canceled
+ Spree::Order.incomplete.not_canceled.includes(order_includes)
end
end
end
diff --git a/core/app/finders/spree/orders/finder_helper.rb b/core/app/finders/spree/orders/finder_helper.rb
new file mode 100644
index 0000000000..53e7e85225
--- /dev/null
+++ b/core/app/finders/spree/orders/finder_helper.rb
@@ -0,0 +1,17 @@
+module Spree
+ module Orders
+ module FinderHelper
+ def order_includes
+ {
+ line_items: [
+ variant: [
+ :product,
+ :images,
+ { option_values: :option_type }
+ ]
+ ]
+ }
+ end
+ end
+ end
+end