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
Fix option value queries to use variants instead of variants_including_master
Damian Legawiec6f278c4eca4a
diff --git a/api/app/services/spree/api/v3/filters_aggregator.rb b/api/app/services/spree/api/v3/filters_aggregator.rbindex e8c84beb13..99b08257af 100644--- a/api/app/services/spree/api/v3/filters_aggregator.rb+++ b/api/app/services/spree/api/v3/filters_aggregator.rb@@ -92,8 +92,15 @@ module Spreeenddef option_value_data(option_type, option_value)- # with_option_value uses group, so we need to count the keys or use a different approach- count = @scope.with_option_value(option_type.name, option_value.name).length+ # Count products in scope that have this option value+ # We use a subquery approach to avoid GROUP BY conflicts when scope includes joins (like in_taxon)+ # Join directly to option_value_variants for efficiency (skips joining through option_values table)+ count = Spree::Product+ .where(id: base_scope_product_ids)+ .joins(:option_value_variants)+ .where(Spree::OptionValueVariant.table_name => { option_value_id: option_value.id })+ .distinct+ .count{id: option_value.prefix_id,@@ -104,6 +111,10 @@ module Spree}end+ def base_scope_product_ids+ @base_scope_product_ids ||= @scope.reorder('').distinct.pluck(:id)+ end+def taxon_filterreturn nil if @taxon.nil?diff --git a/api/spec/controllers/spree/api/v3/store/products/filters_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/products/filters_controller_spec.rbindex 86cd2ee4c9..4869941670 100644--- a/api/spec/controllers/spree/api/v3/store/products/filters_controller_spec.rb+++ b/api/spec/controllers/spree/api/v3/store/products/filters_controller_spec.rb@@ -21,25 +21,21 @@ RSpec.describe Spree::Api::V3::Store::Products::FiltersController, type: :controcreate(:product, stores: [store], status: 'active', taxons: [child_taxon1]).tap do |p|p.option_types << option_type_sizep.option_types << option_type_color- variant = p.master- variant.option_values << option_value_small- variant.option_values << option_value_red+ create(:variant, product: p, option_values: [option_value_small, option_value_red])endendlet!(:product2) docreate(:product, stores: [store], status: 'active', taxons: [child_taxon1]).tap do |p|p.option_types << option_type_size- variant = p.master- variant.option_values << option_value_medium+ create(:variant, product: p, option_values: [option_value_medium])endendlet!(:product3) docreate(:product, stores: [store], status: 'active', taxons: [child_taxon2]).tap do |p|p.option_types << option_type_color- variant = p.master- variant.option_values << option_value_blue+ create(:variant, product: p, option_values: [option_value_blue])endenddiff --git a/core/app/models/concerns/spree/product_scopes.rb b/core/app/models/concerns/spree/product_scopes.rbindex 73f452a173..1576584b64 100644--- a/core/app/models/concerns/spree/product_scopes.rb+++ b/core/app/models/concerns/spree/product_scopes.rb@@ -205,7 +205,7 @@ module Spreereturn Product.group("#{Spree::Product.table_name}.id").none if option_type_id.blank?group("#{Spree::Product.table_name}.id").- joins(variants_including_master: :option_values).+ joins(variants: :option_values).where(Spree::OptionValue.table_name => { name: value, option_type_id: option_type_id })end@@ -223,7 +223,7 @@ module Spreereturn none if actual_ids.empty?group("#{Spree::Product.table_name}.id").- joins(variants_including_master: :option_values).+ joins(variants: :option_values).where(Spree::OptionValue.table_name => { id: actual_ids })end@@ -231,7 +231,7 @@ module Spree# 1) have an option value with the name matching the one given# 2) have a product property with a value matching the one givenadd_search_scope :with do |value|- includes(variants_including_master: :option_values).+ includes(variants: :option_values).includes(:product_properties).where("#{OptionValue.table_name}.name = ? OR #{ProductProperty.table_name}.value = ?", value, value)enddiff --git a/storefront/app/helpers/spree/filters_helper.rb b/storefront/app/helpers/spree/filters_helper.rbindex 41f5581bde..ef24ad9ec6 100644--- a/storefront/app/helpers/spree/filters_helper.rb+++ b/storefront/app/helpers/spree/filters_helper.rb@@ -26,7 +26,7 @@ module Spreedef product_filters_aggregations@product_filters_aggregations ||= Rails.cache.fetch(["product-filters-aggregations", storefront_products_for_filters.cache_key_with_version], expires_in: 1.day) dostorefront_products_for_filters.- joins(variants_including_master: :option_values).+ joins(variants: :option_values).group("#{Spree::OptionValue.table_name}.id").distinct.count@@ -36,7 +36,7 @@ module Spreedef product_single_filter_aggregations@product_single_filter_aggregations ||= Rails.cache.fetch(["product-single-filter-aggregations", default_storefront_products_for_filters.cache_key_with_version], expires_in: 1.day) dodefault_storefront_products_for_filters.- joins(variants_including_master: :option_values).+ joins(variants: :option_values).group("#{Spree::OptionValue.table_name}.id").distinct.count