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.

Fixed N+1s for cart pane (#13043)

Damian Legawiecf6caee5e4591
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index ff5ce175b4..e7c83816a7 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -346,11 +346,11 @@ module Spree
# Returns default Image for Product
# @return [Spree::Image]
def default_image
- @default_image ||= if images.size.positive?
+ @default_image ||= if images.any?
images.first
- elsif default_variant.images.size.positive?
+ elsif default_variant.images.any?
default_variant.default_image
- elsif variant_images.size.positive?
+ elsif variant_images.any?
variant_images.first
end
end
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index dea6647a7d..0279b52f61 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -266,7 +266,7 @@ module Spree
# Returns default Image for Variant
# @return [Spree::Image]
def default_image
- @default_image ||= if images.size.positive?
+ @default_image ||= if images.any?
images.first
else
product.default_image
diff --git a/storefront/app/controllers/concerns/spree/cart_methods.rb b/storefront/app/controllers/concerns/spree/cart_methods.rb
index fb431e786b..382b49e71e 100644
--- a/storefront/app/controllers/concerns/spree/cart_methods.rb
+++ b/storefront/app/controllers/concerns/spree/cart_methods.rb
@@ -30,11 +30,25 @@ module Spree
def load_line_items
@line_items = if @order.nil? || @order.new_record?
[]
- elsif @order.line_items.loaded?
- @order.line_items.sort_by(&:created_at).reverse
else
- @order.line_items.includes(variant: { option_values: [:option_type] }).order(created_at: :desc)
+ @order.line_items.includes(line_items_includes).order(created_at: :desc)
end
end
+
+ def line_items_includes
+ {
+ variant: [
+ :images,
+ { option_values: [:option_type] },
+ {
+ product: [
+ :variants,
+ :variant_images,
+ { master: :images }
+ ]
+ }
+ ]
+ }
+ end
end
end