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
Reduce N+1s for products CSV export
Damian Legawiecda73ea9f1da6
diff --git a/core/app/models/spree/exports/products.rb b/core/app/models/spree/exports/products.rbindex 71592d8e10..27825c8017 100644--- a/core/app/models/spree/exports/products.rb+++ b/core/app/models/spree/exports/products.rb@@ -3,14 +3,14 @@ module Spreeclass Products < Spree::Export# to avoid N+1 queriesdef scope_includes- includes = [:tax_category, :master, { taxons: :taxonomy }, { variants_including_master: variant_includes }]+ includes = [:tax_category, :master, :option_types, { taxons: :taxonomy }, { variants_including_master: variant_includes }]includes << { product_properties: [:property] } if Spree::Config[:product_properties_enabled]includes << { metafields: :metafield_definition }includesenddef variant_includes- [:images, :prices, :stock_items, { option_values: [:option_type] }]+ [:images, :prices, :stock_items, :stock_locations, { option_values: [:option_type] }]enddef multi_line_csv?diff --git a/core/app/presenters/spree/csv/metafields_helper.rb b/core/app/presenters/spree/csv/metafields_helper.rbindex 1253cdcaff..550f3d516a 100644--- a/core/app/presenters/spree/csv/metafields_helper.rb+++ b/core/app/presenters/spree/csv/metafields_helper.rb@@ -3,6 +3,10 @@ module Spreemodule MetafieldsHelperprivate+ def metafield_definitions_for_csv(resource)+ Spree::MetafieldDefinition.for_resource_type(resource.class.to_s).order(:namespace, :key)+ end+def metafields_for_csv(resource)Spree::MetafieldDefinition.for_resource_type(resource.class.to_s).order(:namespace, :key).map do |mf_def|resource.metafields.find { |mf| mf.metafield_definition_id == mf_def.id }&.csv_valuediff --git a/core/app/presenters/spree/csv/product_variant_presenter.rb b/core/app/presenters/spree/csv/product_variant_presenter.rbindex c335bd239c..a9139775af 100644--- a/core/app/presenters/spree/csv/product_variant_presenter.rb+++ b/core/app/presenters/spree/csv/product_variant_presenter.rb@@ -72,6 +72,8 @@ module Spree## @return [Array] An array containing the combined product and variant CSV data.def call+ total_on_hand = variant.total_on_hand+csv = [product.id,variant.sku,@@ -87,7 +89,7 @@ module Spreeindex.zero? ? product.tag_list.to_s : nil,index.zero? ? product.label_list.to_s : nil,variant.amount_in(currency).to_f,- variant.compare_at_price&.to_f,+ variant.compare_at_amount_in(currency).to_f,currency,variant.width,variant.height,@@ -98,7 +100,7 @@ module Spreevariant.available_on&.strftime('%Y-%m-%d %H:%M:%S'),(variant.discontinue_on || product.discontinue_on)&.strftime('%Y-%m-%d %H:%M:%S'),variant.track_inventory?,- variant.total_on_hand == BigDecimal::INFINITY ? '∞' : variant.total_on_hand,+ total_on_hand == BigDecimal::INFINITY ? '∞' : total_on_hand,variant.backorderable?,variant.tax_category&.name,variant.digital?,