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.

V-3066 FIX storefront filters (#12641)

brozek3f467c96ba50
diff --git a/core/app/finders/spree/products/find.rb b/core/app/finders/spree/products/find.rb
index 01997389dc..1c54a7f293 100644
--- a/core/app/finders/spree/products/find.rb
+++ b/core/app/finders/spree/products/find.rb
@@ -11,6 +11,7 @@ module Spree
@currency = params.dig(:filter, :currency) || params[:currency] || Spree::Store.default.default_currency
@taxons = taxon_ids(params.dig(:filter, :taxons))
@concat_taxons = taxon_ids(params.dig(:filter, :concat_taxons))
+ @taxonomies = params.dig(:filter, :taxonomy_ids).to_h
@name = params.dig(:filter, :name)
@slug = params.dig(:filter, :slug)
@options = params.dig(:filter, :options).try(:to_unsafe_hash)
@@ -52,6 +53,7 @@ module Spree
products = show_only_backorderable(products)
products = show_only_purchasable(products)
products = show_only_out_of_stock(products)
+ products = by_taxonomies(products)
products = ordered(products)
products = by_vendor_ids(products)
@@ -62,7 +64,7 @@ module Spree
attr_reader :ids, :skus, :price, :currency, :taxons, :concat_taxons, :name, :options, :option_value_ids, :scope,
:sort_by, :deleted, :discontinued, :properties, :store, :in_stock, :backorderable, :purchasable, :tags,
- :query, :vendor_ids, :out_of_stock, :slug
+ :query, :vendor_ids, :out_of_stock, :slug, :taxonomies
def query?
query.present?
@@ -169,6 +171,21 @@ module Spree
products.where(id: product_ids)
end
+ def by_taxonomies(products)
+ return products if taxonomies.none?
+
+ taxon_groups = taxonomies.values.map { |taxonomy| taxon_ids(taxonomy[:taxon_ids].join(',')) }.compact_blank
+
+ return products if taxon_groups.empty?
+
+ taxonomies_products = products.joins(:classifications).where(Classification.table_name => { taxon_id: taxon_groups.flatten.uniq })
+
+ # No need to filter if there is only one taxonomy
+ return taxonomies_products if taxonomies.size == 1
+
+ products.where(id: products_matching_all_taxonomies_ids(taxonomies_products.ids, taxon_groups))
+ end
+
def by_name(products)
return products unless name?
@@ -336,6 +353,16 @@ module Spree
def order_by_best_selling(scope)
scope.by_best_selling(:desc)
end
+
+ def products_matching_all_taxonomies_ids(products_ids, taxon_groups)
+ classifications = Spree::Classification.grouped_taxon_ids_for_products(products_ids, taxon_groups.flatten)
+ classifications_hash = classifications.to_h.transform_values { |taxon_ids| taxon_ids.split(',') }
+
+ # Find products ids that match all taxonomies to tighten filter results
+ classifications_hash.filter_map do |product_id, product_taxon_ids|
+ product_id if taxon_groups.all? { |group| group.intersect?(product_taxon_ids) }
+ end
+ end
end
end
end
diff --git a/core/app/models/spree/classification.rb b/core/app/models/spree/classification.rb
index 2431d662a7..c3802ac224 100644
--- a/core/app/models/spree/classification.rb
+++ b/core/app/models/spree/classification.rb
@@ -23,5 +23,18 @@ module Spree
group("#{Spree::Classification.table_name}.id").
reorder(completed_orders_count: order_direction, completed_orders_total: order_direction)
}
+
+ scope :grouped_taxon_ids_for_products, lambda { |product_ids, taxon_groups|
+ where(product_id: product_ids, taxon_id: taxon_groups).
+ group(:product_id).
+ then do |query|
+ case ActiveRecord::Base.connection.adapter_name
+ when 'PostgreSQL'
+ query.pluck(:product_id, Arel.sql("STRING_AGG(taxon_id::text, ',')"))
+ when 'Mysql2', 'SQLite'
+ query.pluck(:product_id, Arel.sql('GROUP_CONCAT(taxon_id)'))
+ end
+ end
+ }
end
end
diff --git a/core/spec/models/spree/classification_spec.rb b/core/spec/models/spree/classification_spec.rb
index 922da769a4..607283917b 100644
--- a/core/spec/models/spree/classification_spec.rb
+++ b/core/spec/models/spree/classification_spec.rb
@@ -90,5 +90,39 @@ module Spree
expect positions_to_be_valid(taxon_with_5_products)
end
end
+
+ describe '.grouped_taxon_ids_for_products' do
+ let(:taxon1) { create(:taxon) }
+ let(:taxon2) { create(:taxon) }
+ let(:taxon3) { create(:taxon) }
+ let(:product1) { create(:product) }
+ let(:product2) { create(:product) }
+ let!(:classification1) { create(:classification, taxon: taxon1, product: product1) }
+ let!(:classification2) { create(:classification, taxon: taxon2, product: product1) }
+ let!(:classification3) { create(:classification, taxon: taxon3, product: product2) }
+ let(:expected_result) { [[product1.id, "#{taxon1.id},#{taxon2.id}"], [product2.id, taxon3.id.to_s]] }
+ let(:product_ids) { [product1.id, product2.id] }
+ let(:taxon_groups) { [taxon1.id, taxon2.id, taxon3.id] }
+
+ it 'returns the correct taxon ids' do
+ expect(described_class.grouped_taxon_ids_for_products(product_ids, taxon_groups)).to eq(expected_result)
+ end
+
+ context 'when empty taxon groups' do
+ let(:taxon_groups) { [] }
+
+ it 'returns an empty array' do
+ expect(described_class.grouped_taxon_ids_for_products(product_ids, taxon_groups)).to eq([])
+ end
+ end
+
+ context 'when empty product ids' do
+ let(:product_ids) { [] }
+
+ it 'returns an empty array' do
+ expect(described_class.grouped_taxon_ids_for_products(product_ids, taxon_groups)).to eq([])
+ end
+ end
+ end
end
end
diff --git a/storefront/app/controllers/spree/store_controller.rb b/storefront/app/controllers/spree/store_controller.rb
index d94f56bc1c..35a6fce0d7 100644
--- a/storefront/app/controllers/spree/store_controller.rb
+++ b/storefront/app/controllers/spree/store_controller.rb
@@ -73,7 +73,10 @@ module Spree
:out_of_stock,
{
options: [store_filter_names_hash],
- taxon_ids: []
+ taxon_ids: [],
+ taxonomy_ids: [
+ taxon_ids: []
+ ]
}
]
)
diff --git a/storefront/app/helpers/spree/filters_helper.rb b/storefront/app/helpers/spree/filters_helper.rb
index afd66de300..506553fd51 100644
--- a/storefront/app/helpers/spree/filters_helper.rb
+++ b/storefront/app/helpers/spree/filters_helper.rb
@@ -92,7 +92,7 @@ module Spree
end
def storefront_products_for_taxon_filters
- @storefront_products_for_taxon_filters ||= products_for_filters_scope(except_filters: [:taxons])
+ @storefront_products_for_taxon_filters ||= products_for_filters_scope(except_filters: [:taxons, :taxonomy_ids])
end
def filter_taxon_ids
diff --git a/storefront/app/views/themes/default/spree/products/filters/_taxons.erb b/storefront/app/views/themes/default/spree/products/filters/_taxons.erb
index aebd09398f..67faf48217 100644
--- a/storefront/app/views/themes/default/spree/products/filters/_taxons.erb
+++ b/storefront/app/views/themes/default/spree/products/filters/_taxons.erb
@@ -5,7 +5,7 @@
class="st-accordion"
data-controller='accordion'
data-accordion-close-others-value="false">
- <%= link_to "#taxonomy_filter_#{taxonomy.id}", class: "cursor-pointer uppercase flex items-center justify-between h-10 px-4 lg:px-10 text-sm tracking-widest focus:outline-none transition duration-150 ease-in-out #{'st-accordion__icon--opened' if params[:filter].nil? || current_taxon.present? || permitted_products_params.dig(:filter, :taxon_ids)&.present?}", data: { action: 'accordion#toggle:prevent' } do %>
+ <%= link_to "#taxonomy_filter_#{taxonomy.id}", class: "cursor-pointer uppercase flex items-center justify-between h-10 px-4 lg:px-10 text-sm tracking-widest focus:outline-none transition duration-150 ease-in-out #{'st-accordion__icon--opened' if params[:filter].nil? || current_taxon.present? || permitted_products_params.dig(:filter, :taxonomy_ids, taxonomy.id.to_s, :taxon_ids).present?}", data: { action: 'accordion#toggle:prevent' } do %>
<%= taxonomy.name.singularize %>
<%= render 'spree/shared/icons/chevron_down' %>
<% end %>
@@ -27,10 +27,10 @@
<% first_taxons.each do |taxon| %>
<li>
<%= f.label "filter_taxon_#{taxon.id}", class: "flex items-center gap-2 cursor-pointer group text-sm h-10", data: { searchable_list_target: "item", text: taxon.name } do %>
- <%= f.check_box "filter[taxon_ids][]",
+ <%= f.check_box "filter[taxonomy_ids][#{taxonomy.id}][taxon_ids][]",
{
id: "filter_taxon_#{taxon.id}",
- checked: permitted_products_params.dig(:filter, :taxon_ids)&.include?(taxon.id.to_s),
+ checked: permitted_products_params.dig(:filter, :taxonomy_ids, taxonomy.id.to_s, :taxon_ids)&.include?(taxon.id.to_s),
class: "input-checkbox group-focus-within:outline"
},
taxon.id,
@@ -49,10 +49,10 @@
<% rest_of_the_taxons.each do |taxon| %>
<li class="px-4 lg:px-10">
<%= f.label "filter_taxon_#{taxon.id}", class: "flex items-center gap-2 cursor-pointer group text-sm h-10", data: { searchable_list_target: "item", text: taxon.name } do %>
- <%= f.check_box "filter[taxon_ids][]",
+ <%= f.check_box "filter[taxonomy_ids][#{taxonomy.id}][taxon_ids][]",
{
id: "filter_taxon_#{taxon.id}",
- checked: permitted_products_params.dig(:filter, :taxon_ids)&.include?(taxon.id.to_s),
+ checked: permitted_products_params.dig(:filter, :taxonomy_ids, taxonomy.id.to_s, :taxon_ids)&.include?(taxon.id.to_s),
class: "input-checkbox group-focus-within:outline"
},
taxon.id,
diff --git a/storefront/spec/controllers/spree/products_controller_spec.rb b/storefront/spec/controllers/spree/products_controller_spec.rb
index f95553e005..6d3b0f8408 100644
--- a/storefront/spec/controllers/spree/products_controller_spec.rb
+++ b/storefront/spec/controllers/spree/products_controller_spec.rb
@@ -16,6 +16,142 @@ describe Spree::ProductsController, type: :controller do
expect(response).to render_template(:index)
expect(assigns(:current_page)).to be_a(Spree::Pages::ShopAll)
end
+
+ context 'when filtering' do
+ let(:taxon) { create(:taxon) }
+
+ context 'by one taxon' do
+ let(:product) { create(:product, stores: [store], taxons: [taxon]) }
+
+ before do
+ product
+ get :index, params: { filter: { taxon_ids: [taxon.id] } }
+ end
+
+ it 'returns one product' do
+ expect(assigns(:storefront_products).records).to eq([product])
+ end
+ end
+
+ context 'by taxonomies' do
+ let(:taxonomy) { create(:taxonomy) }
+ let(:taxon) { create(:taxon, taxonomy: taxonomy) }
+ let(:taxon2) { create(:taxon, taxonomy: taxonomy) }
+ let(:product) { create(:product, stores: [store], taxons: [taxon]) }
+ let(:product2) { create(:product, stores: [store], taxons: [taxon2]) }
+
+ before do
+ product
+ product2
+ get :index, params: { filter: { taxonomy_ids: taxonomy_ids } }
+ end
+
+ context 'when one taxonomy is selected' do
+ context 'and both taxons are selected' do
+ let(:taxonomy_ids) { { taxonomy.id => { taxon_ids: [taxon.id, taxon2.id] } } }
+
+ it 'returns both products' do
+ expect(assigns(:storefront_products).records).to eq([product, product2])
+ end
+ end
+
+ context 'and only one taxon is selected' do
+ let(:taxonomy_ids) { { taxonomy.id => { taxon_ids: [taxon.id] } } }
+
+ it 'returns only the product associated with the selected taxon' do
+ expect(assigns(:storefront_products).records).to eq([product])
+ end
+ end
+
+ context 'when no taxons are selected' do
+ let(:taxonomy_ids) { { taxonomy.id => { taxon_ids: [] } } }
+
+ it 'returns all products' do
+ expect(assigns(:storefront_products).records).to eq([product, product2])
+ end
+ end
+ end
+
+ context 'when multiple taxonomies are selected' do
+ let(:taxonomy2) { create(:taxonomy) }
+ let(:taxon3) { create(:taxon, taxonomy: taxonomy2) }
+ let(:product3) { create(:product, stores: [store], taxons: [taxon3, taxon]) }
+
+ before do
+ product3
+ get :index, params: { filter: { taxonomy_ids: taxonomy_ids } }
+ end
+
+ context 'and product matches both taxons from both taxonomies exactly' do
+ let(:taxonomy_ids) { { taxonomy.id => { taxon_ids: [taxon.id] }, taxonomy2.id => { taxon_ids: [taxon3.id] } } }
+
+ it 'returns products associated with the selected taxons' do
+ expect(assigns(:storefront_products).records).to eq([product3])
+ end
+ end
+
+ context 'and product matches only one taxonomy completely' do
+ let(:taxonomy_ids) { { taxonomy.id => { taxon_ids: [taxon.id] }, taxonomy2.id => { taxon_ids: [taxon3.id] } } }
+ let(:product3) { create(:product, stores: [store], taxons: [taxon3]) }
+
+ it 'returns no products' do
+ expect(assigns(:storefront_products).records).to eq([])
+ end
+ end
+
+ context 'and product matches none of the taxons' do
+ let(:taxonomy_ids) { { taxonomy.id => { taxon_ids: [taxon.id] }, taxonomy2.id => { taxon_ids: [taxon3.id] } } }
+ let(:product3) { create(:product, stores: [store], taxons: [taxon2]) }
+
+ it 'returns no products' do
+ expect(assigns(:storefront_products).records).to eq([])
+ end
+ end
+
+ context 'and product matches all taxons from one taxonomy and partially from another' do
+ let(:taxonomy_ids) { { taxonomy.id => { taxon_ids: [taxon.id] }, taxonomy2.id => { taxon_ids: [taxon2.id, taxon3.id] } } }
+ let(:product3) { create(:product, stores: [store], taxons: [taxon, taxon2]) }
+
+ it 'returns products associated with the selected taxons' do
+ expect(assigns(:storefront_products).records).to eq([product3])
+ end
+ end
+
+ context 'and product matches one taxon from each taxonomy' do
+ let(:taxonomy_ids) { { taxonomy.id => { taxon_ids: [taxon.id, taxon2.id] }, taxonomy2.id => { taxon_ids: [taxon3.id, taxon4.id] } } }
+ let(:taxon4) { create(:taxon, taxonomy: taxonomy2) }
+ let(:product3) { create(:product, stores: [store], taxons: [taxon, taxon3]) }
+
+ it 'returns products associated with the selected taxons' do
+ expect(assigns(:storefront_products).records).to eq([product3])
+ end
+ end
+
+ context 'and product matches three taxons from three taxonomies' do
+ let(:taxonomy_ids) { { taxonomy.id => { taxon_ids: [taxon.id, taxon2.id] }, taxonomy2.id => { taxon_ids: [taxon3.id, taxon4.id] }, taxonomy3.id => { taxon_ids: [taxon5.id] } } }
+ let(:taxonomy3) { create(:taxonomy) }
+ let(:taxon4) { create(:taxon, taxonomy: taxonomy2) }
+ let(:taxon5) { create(:taxon, taxonomy: taxonomy3) }
+ let(:product3) { create(:product, stores: [store], taxons: [taxon, taxon3, taxon5]) }
+
+ it 'returns products associated with the selected taxons' do
+ expect(assigns(:storefront_products).records).to eq([product3])
+ end
+ end
+
+ context 'and product matches taxon from parent' do
+ let(:taxonomy_ids) { { taxonomy.id => { taxon_ids: [taxon.id, taxon2.id] }, taxonomy2.id => { taxon_ids: [taxon4.id] } } }
+ let(:taxon3) { create(:taxon, taxonomy: taxonomy2, parent_id: taxon4.id) }
+ let(:taxon4) { create(:taxon, taxonomy: taxonomy2) }
+ let(:product3) { create(:product, stores: [store], taxons: [taxon, taxon3]) }
+
+ it 'returns products associated with the selected taxons' do
+ expect(assigns(:storefront_products).records).to eq([product3])
+ end
+ end
+ end
+ end
+ end
end
describe '#show' do