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.

Return filter options with products API (#11318)

Rafał Cymeryse8d04eaf18cd
diff --git a/api/app/controllers/spree/api/v2/storefront/products_controller.rb b/api/app/controllers/spree/api/v2/storefront/products_controller.rb
index ffb252b1f8..6e911dacf6 100644
--- a/api/app/controllers/spree/api/v2/storefront/products_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/products_controller.rb
@@ -46,6 +46,14 @@ module Spree
def allowed_sort_attributes
super << :available_on
end
+
+ def collection_meta(collection)
+ super(collection).merge(filters: filters_meta)
+ end
+
+ def filters_meta
+ Spree::Api::Products::FiltersPresenter.new(current_store, current_currency, params).to_h
+ end
end
end
end
diff --git a/api/app/presenters/spree/api/products/filters_presenter.rb b/api/app/presenters/spree/api/products/filters_presenter.rb
new file mode 100644
index 0000000000..5541ceac27
--- /dev/null
+++ b/api/app/presenters/spree/api/products/filters_presenter.rb
@@ -0,0 +1,39 @@
+module Spree
+ module Api
+ module Products
+ class FiltersPresenter
+ def initialize(current_store, current_currency, params)
+ @products_for_filters = find_products_for_filters(current_store, current_currency, params)
+ end
+
+ def to_h
+ option_values = Spree::OptionValues::FindAvailable.new(products_scope: products_for_filters).execute
+ option_values_presenters = Spree::Filters::OptionsPresenter.new(option_values_scope: option_values).to_a
+ product_properties = Spree::ProductProperties::FindAvailable.new(products_scope: products_for_filters).execute
+ product_properties_presenters = Spree::Filters::PropertiesPresenter.new(product_properties_scope: product_properties).to_a
+ {
+ option_types: option_values_presenters.map(&:to_h),
+ product_properties: product_properties_presenters.map(&:to_h)
+ }
+ end
+
+ private
+
+ attr_reader :products_for_filters
+
+ def find_products_for_filters(current_store, current_currency, params)
+ current_taxons = find_current_taxons(current_store, params)
+ current_store.products.active(current_currency).in_taxons(current_taxons)
+ end
+
+ def find_current_taxons(current_store, params)
+ taxons_param = params.dig(:filter, :taxons)
+ return nil if taxons_param.nil? || taxons_param.to_s.blank?
+
+ taxon_ids = taxons_param.to_s.split(',')
+ current_store.taxons.where(id: taxon_ids)
+ end
+ end
+ end
+ end
+end
diff --git a/api/spec/requests/spree/api/v2/storefront/products_spec.rb b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
index 1263c259ff..0737c0d07c 100644
--- a/api/spec/requests/spree/api/v2/storefront/products_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
@@ -613,6 +613,166 @@ describe 'API V2 Storefront Products Spec', type: :request do
end
end
+ context 'return filter metadata' do
+ let!(:option_type2) { create(:option_type) }
+ let!(:option_type2_value1) { create(:option_value, option_type: option_type2 )}
+ let!(:option_type2_value2) { create(:option_value, option_type: option_type2 )}
+
+ let(:product_with_option_type2) { create(:product, option_types: [option_type2], stores: [store]) }
+ let!(:product_with_option_type2_variant1) { create(:variant, product: product_with_option_type2, option_values: [option_type2_value1]) }
+ let!(:product_with_option_type2_variant2) { create(:variant, product: product_with_option_type2, option_values: [option_type2_value2]) }
+
+ let!(:property2) { create(:property, :filterable) }
+ let!(:property3) { create(:property, :filterable) }
+ let!(:product_property2) { create(:product_property, property: property2, product: product_with_property, value: 'A property') }
+ let!(:product2_property2) { create(:product_property, property: property2, product: product_with_option_type2, value: 'Test') }
+ let!(:product2_property3) { create(:product_property, property: property3, product: product_with_option_type2, value: 'Test') }
+
+ let!(:unused_option_type) { create(:option_type) }
+ let!(:unused_option_value) { create(:option_value) }
+
+ let(:option_type1_response) do
+ {
+ id: option_type.id,
+ name: option_type.name,
+ presentation: option_type.presentation,
+ option_values: [
+ {
+ id: option_value.id,
+ name: option_value.name,
+ presentation: option_value.presentation,
+ position: option_value.position
+ }
+ ]
+ }
+ end
+
+ let(:option_type2_response) do
+ {
+ id: option_type2.id,
+ name: option_type2.name,
+ presentation: option_type2.presentation,
+ option_values: [
+ {
+ id: option_type2_value1.id,
+ name: option_type2_value1.name,
+ presentation: option_type2_value1.presentation,
+ position: option_type2_value1.position
+ },
+ {
+ id: option_type2_value2.id,
+ name: option_type2_value2.name,
+ presentation: option_type2_value2.presentation,
+ position: option_type2_value2.position
+ }
+ ]
+ }
+ end
+
+ let(:property2_response) do
+ {
+ id: property2.id,
+ name: property2.name,
+ presentation: property2.presentation,
+ values: [
+ {
+ value: product_property2.value,
+ filter_param: product_property2.filter_param,
+ },
+ {
+ value: product2_property2.value,
+ filter_param: product2_property2.filter_param,
+ }
+ ]
+ }
+ end
+
+ let(:property3_response) do
+ {
+ id: property3.id,
+ name: property3.name,
+ presentation: property3.presentation,
+ values: [
+ {
+ value: product2_property3.value,
+ filter_param: product2_property3.filter_param
+ }
+ ]
+ }
+ end
+
+ context 'when no filters are applied' do
+ before { get '/api/v2/storefront/products' }
+
+ it 'returns list of available filters for all products' do
+ expect(json_response['meta']['filters']['option_types'].count).to eq 2
+ expect(json_response['meta']['filters']['option_types']).to contain_exactly(option_type1_response, option_type2_response)
+ expect(json_response['meta']['filters']['product_properties'].count).to eq 2
+ expect(json_response['meta']['filters']['product_properties']).to contain_exactly(property2_response, property3_response)
+ end
+ end
+
+ context 'when filter by option type is applied' do
+ before { get "/api/v2/storefront/products?filter[options][#{option_type.name}]=#{option_value.name}" }
+
+ it 'returns list of all available filters for products' do
+ expect(json_response['meta']['filters']['option_types'].count).to eq 2
+ expect(json_response['meta']['filters']['option_types']).to contain_exactly(option_type1_response, option_type2_response)
+ expect(json_response['meta']['filters']['product_properties'].count).to eq 2
+ expect(json_response['meta']['filters']['product_properties']).to contain_exactly(property2_response, property3_response)
+ end
+ end
+
+ context 'when filter by product property is applied' do
+ before { get "/api/v2/storefront/products?filter[properties][#{property.filter_param}]=#{product_property.filter_param}" }
+
+ it 'returns list of all available filters for products' do
+ expect(json_response['meta']['filters']['option_types'].count).to eq 2
+ expect(json_response['meta']['filters']['option_types']).to contain_exactly(option_type1_response, option_type2_response)
+ expect(json_response['meta']['filters']['product_properties'].count).to eq 2
+ expect(json_response['meta']['filters']['product_properties']).to contain_exactly(property2_response, property3_response)
+ end
+ end
+
+ context 'when filter by taxon is applied' do
+ let(:product_with_taxon_and_options) { create(:product, taxons: [taxon], option_types: [option_type2], stores: [store]) }
+ let!(:product_with_taxon_and_options_property) { create(:product_property, property: property3, product: product_with_taxon_and_options, value: 'Test') }
+ let!(:product_with_taxon_and_options_variant1) { create(:variant, product: product_with_taxon_and_options, option_values: [option_type2_value1]) }
+ let!(:product_with_taxon_and_options_variant2) { create(:variant, product: product_with_taxon_and_options, option_values: [option_type2_value2]) }
+
+ before { get "/api/v2/storefront/products?filter[taxons]=#{taxon.id}" }
+
+ it 'returns list of available filters for given taxon' do
+ expect(json_response['meta']['filters']['option_types'].count).to eq 1
+ expect(json_response['meta']['filters']['option_types']).to contain_exactly(option_type2_response)
+ expect(json_response['meta']['filters']['product_properties'].count).to eq 1
+ expect(json_response['meta']['filters']['product_properties']).to contain_exactly(property3_response)
+ end
+ end
+
+ context 'when filter by multiple taxons is applied' do
+ let(:product_with_taxon_and_options) { create(:product, taxons: [taxon], option_types: [option_type2], stores: [store]) }
+ let!(:product_with_taxon_and_options_property) { create(:product_property, property: property3, product: product_with_taxon_and_options, value: 'Test') }
+ let!(:product_with_taxon_and_options_variant1) { create(:variant, product: product_with_taxon_and_options, option_values: [option_type2_value1]) }
+ let!(:product_with_taxon_and_options_variant2) { create(:variant, product: product_with_taxon_and_options, option_values: [option_type2_value2]) }
+
+ let(:taxonomy2) { create(:taxonomy, store: store) }
+ let(:taxon2) { taxonomy2.root }
+ let(:product_with_taxon2_and_options) { create(:product, taxons: [taxon2], option_types: [option_type2], stores: [store]) }
+ let!(:product_with_taxon2_and_options_property) { create(:product_property, property: property3, product: product_with_taxon2_and_options, value: 'Test') }
+ let!(:product_with_taxon2_and_options_variant1) { create(:variant, product: product_with_taxon2_and_options, option_values: [option_value]) }
+
+ before { get "/api/v2/storefront/products?filter[taxons]=#{taxon.id},#{taxon2.id}" }
+
+ it 'returns list of available filters for given taxons' do
+ expect(json_response['meta']['filters']['option_types'].count).to eq 2
+ expect(json_response['meta']['filters']['option_types']).to contain_exactly(option_type1_response, option_type2_response)
+ expect(json_response['meta']['filters']['product_properties'].count).to eq 1
+ expect(json_response['meta']['filters']['product_properties']).to contain_exactly(property3_response)
+ end
+ end
+ end
+
context 'fetch products by curency param' do
before { store.update(supported_currencies: 'USD,EUR,GBP') }
diff --git a/core/app/presenters/spree/filters/options_presenter.rb b/core/app/presenters/spree/filters/options_presenter.rb
index 682cac5059..0dd0de6b31 100644
--- a/core/app/presenters/spree/filters/options_presenter.rb
+++ b/core/app/presenters/spree/filters/options_presenter.rb
@@ -3,6 +3,26 @@ module Spree
class OptionsPresenter
FilterableOptionType = Struct.new(:option_type, :option_values, keyword_init: true) do
delegate_missing_to :option_type
+
+ def to_h
+ {
+ id: option_type.id,
+ name: option_type.name,
+ presentation: option_type.presentation,
+ option_values: option_values.map { |e| option_value_hash(e) }
+ }
+ end
+
+ private
+
+ def option_value_hash(option_value)
+ {
+ id: option_value.id,
+ name: option_value.name,
+ presentation: option_value.presentation,
+ position: option_value.position
+ }
+ end
end
def initialize(option_values_scope:)
diff --git a/core/app/presenters/spree/filters/property_presenter.rb b/core/app/presenters/spree/filters/property_presenter.rb
index 47bd13937a..5b6aded0c2 100644
--- a/core/app/presenters/spree/filters/property_presenter.rb
+++ b/core/app/presenters/spree/filters/property_presenter.rb
@@ -14,9 +14,29 @@ module Spree
property.uniq_values(product_properties_scope: product_properties)
end
+ def to_h
+ {
+ id: property.id,
+ name: property.name,
+ presentation: property.presentation,
+ values: values_hash
+ }
+ end
+
private
attr_reader :property
+
+ def values_hash
+ value_hashes = uniq_values.map do |filter_param, value|
+ {
+ value: value,
+ filter_param: filter_param
+ }
+ end
+
+ value_hashes.sort_by { |e| e[:value] }
+ end
end
end
end