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.

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.rb
index 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 Spree
end
def 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_filter
return 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.rb
index 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: :contro
create(:product, stores: [store], status: 'active', taxons: [child_taxon1]).tap do |p|
p.option_types << option_type_size
p.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])
end
end
let!(:product2) do
create(: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])
end
end
let!(:product3) do
create(: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])
end
end
diff --git a/core/app/models/concerns/spree/product_scopes.rb b/core/app/models/concerns/spree/product_scopes.rb
index 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 Spree
return 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 Spree
return 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 given
add_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)
end
diff --git a/storefront/app/helpers/spree/filters_helper.rb b/storefront/app/helpers/spree/filters_helper.rb
index 41f5581bde..ef24ad9ec6 100644
--- a/storefront/app/helpers/spree/filters_helper.rb
+++ b/storefront/app/helpers/spree/filters_helper.rb
@@ -26,7 +26,7 @@ module Spree
def product_filters_aggregations
@product_filters_aggregations ||= Rails.cache.fetch(["product-filters-aggregations", storefront_products_for_filters.cache_key_with_version], expires_in: 1.day) do
storefront_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 Spree
def 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) do
default_storefront_products_for_filters.
- joins(variants_including_master: :option_values).
+ joins(variants: :option_values).
group("#{Spree::OptionValue.table_name}.id").
distinct.
count