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
display only these Options for filtering that have any Products for a selected Currency and Taxon
Łukasz Adamczyk9ff9e35c7591
diff --git a/core/app/finders/spree/option_values/find_filterable.rb b/core/app/finders/spree/option_values/find_filterable.rbindex 9735a7cea4..260a2e79d3 100644--- a/core/app/finders/spree/option_values/find_filterable.rb+++ b/core/app/finders/spree/option_values/find_filterable.rb@@ -9,12 +9,16 @@ module Spreedef executescope.filterable.- where(variants: { product_id: products_scope.pluck(:id) })+ where(variants: { product_id: products_ids })endprivateattr_reader :scope, :products_scope++ def products_ids+ products_scope.map(&:id)+ endendendenddiff --git a/core/app/presenters/spree/filters/options.rb b/core/app/presenters/spree/filters/options.rbnew file mode 100644index 0000000000..9f3bd376a2--- /dev/null+++ b/core/app/presenters/spree/filters/options.rb@@ -0,0 +1,27 @@+module Spree+ module Filters+ class Options+ FilterableOptionType = Struct.new(:option_type, :option_values, keyword_init: true) do+ delegate_missing_to :option_type+ end++ def initialize(option_values_scope:)+ @option_values = option_values_scope.includes(:option_type)+ end++ def to_a+ grouped_options.map do |option_type, option_values|+ FilterableOptionType.new(option_type: option_type, option_values: option_values)+ end+ end++ private++ attr_reader :option_values++ def grouped_options+ option_values.group_by(&:option_type)+ end+ end+ end+enddiff --git a/core/spec/presenters/spree/filters/options_spec.rb b/core/spec/presenters/spree/filters/options_spec.rbnew file mode 100644index 0000000000..ac14828f66--- /dev/null+++ b/core/spec/presenters/spree/filters/options_spec.rb@@ -0,0 +1,37 @@+require 'spec_helper'++module Spree+ RSpec.describe Filters::Options do+ let(:options) { described_class.new(option_values_scope: OptionValue.where(id: option_values)) }++ let(:size) { create(:option_type, :size) }+ let(:s_size) { create(:option_value, option_type: size, name: 's') }+ let(:m_size) { create(:option_value, option_type: size, name: 'm') }++ let(:color) { create(:option_type, :color) }+ let(:red_color) { create(:option_value, option_type: color, name: 'red') }+ let(:green_color) { create(:option_value, option_type: color, name: 'green') }+ let(:blue_color) { create(:option_value, option_type: color, name: 'blue') }++ let(:option_values) do+ [+ s_size, m_size,+ red_color, green_color, blue_color+ ]+ end++ describe '#to_a' do+ subject(:filterable_options) { options.to_a }++ it 'returns filterable Option Types and Values' do+ aggregate_failures 'filterable options' do+ size_option = filterable_options.find { |option| option.option_type == size }+ expect(size_option.option_values).to contain_exactly(s_size, m_size)++ color_option = filterable_options.find { |option| option.option_type == color }+ expect(color_option.option_values).to contain_exactly(red_color, green_color, blue_color)+ end+ end+ end+ end+enddiff --git a/frontend/app/helpers/spree/frontend_helper.rb b/frontend/app/helpers/spree/frontend_helper.rbindex 79cc8a6757..d04adce765 100644--- a/frontend/app/helpers/spree/frontend_helper.rb+++ b/frontend/app/helpers/spree/frontend_helper.rb@@ -299,7 +299,10 @@ module Spreedef available_option_types@available_option_types ||= Rails.cache.fetch("available-option-types/#{available_option_types_cache_key}") do- Spree::OptionType.includes(:option_values).filterable.to_a+ products_scope = Product.active(current_currency).in_taxon(@taxon)+ option_values = OptionValues::FindFilterable.new(products_scope: products_scope).execute++ Filters::Options.new(option_values: option_values).to_aend@available_option_typesend