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.

Added filtering by properties in Storefront API Products endpoints

Damian Legawiec834cee23bf47
diff --git a/api/app/serializers/spree/v2/storefront/product_property_serializer.rb b/api/app/serializers/spree/v2/storefront/product_property_serializer.rb
index fe6e03288e..d50639411c 100644
--- a/api/app/serializers/spree/v2/storefront/product_property_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/product_property_serializer.rb
@@ -4,7 +4,7 @@ module Spree
class ProductPropertySerializer < BaseSerializer
set_type :product_property
- attribute :value
+ attribute :value, :filter_param
attribute :name do |product_property|
product_property.property_name
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 946fe27b4d..a55e5c6be1 100644
--- a/api/spec/requests/spree/api/v2/storefront/products_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
@@ -15,6 +15,9 @@ describe 'API V2 Storefront Products Spec', type: :request do
let!(:deleted_product) { create(:product, deleted_at: Time.current - 1.day) }
let!(:discontinued_product) { create(:product, discontinue_on: Time.current - 1.day) }
let!(:not_available_product) { create(:product, available_on: nil) }
+ let!(:property) { create(:property) }
+ let!(:product_with_property) { create(:product, properties: [property]) }
+ let!(:product_property) { create(:product_property, property: property, product: product_with_property, value: 'Some Value') }
before { Spree::Api::Config[:api_v2_per_page_limit] = 4 }
@@ -96,6 +99,19 @@ describe 'API V2 Storefront Products Spec', type: :request do
end
end
+ context 'with specified properties' do
+ before { get "/api/v2/storefront/products?filter[properties][#{property.id}]=#{product_property.filter_param}&include=product_properties" }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products with specified options' do
+ expect(json_response['data'].first).to have_id(product_with_property.id.to_s)
+ expect(json_response['included']).to include(have_type('product_property').and(have_attribute(:name).with_value(property.name)))
+ expect(json_response['included']).to include(have_type('product_property').and(have_attribute(:value).with_value(product_property.value)))
+ expect(json_response['included']).to include(have_type('product_property').and(have_attribute(:filter_param).with_value(product_property.filter_param)))
+ end
+ end
+
context 'with specified multiple filters' do
before { get "/api/v2/storefront/products?filter[name]=#{product_with_name.name}&filter[price]=#{product_with_name.price.to_f - 0.02},#{product_with_name.price.to_f + 0.02}" }
@@ -113,7 +129,7 @@ describe 'API V2 Storefront Products Spec', type: :request do
it_behaves_like 'returns 200 HTTP status'
it 'returns products with deleted products' do
- expect(json_response['data'].count).to eq 7
+ expect(json_response['data'].count).to eq 8
expect(json_response['data'].pluck(:id)).to include(deleted_product.id.to_s)
end
end
@@ -124,7 +140,7 @@ describe 'API V2 Storefront Products Spec', type: :request do
it_behaves_like 'returns 200 HTTP status'
it 'returns products with discontinued products' do
- expect(json_response['data'].count).to eq 8
+ expect(json_response['data'].count).to eq 9
expect(json_response['data'].pluck(:id)).to include(discontinued_product.id.to_s)
end
end
@@ -137,7 +153,7 @@ describe 'API V2 Storefront Products Spec', type: :request do
it_behaves_like 'returns 200 HTTP status'
it 'returns available, deleted and discontinued products' do
- expect(json_response['data'].count).to eq 9
+ expect(json_response['data'].count).to eq 10
expect(json_response['data'].pluck(:id)).to include(deleted_product.id.to_s, discontinued_product.id.to_s)
end
end
@@ -219,7 +235,7 @@ describe 'API V2 Storefront Products Spec', type: :request do
before { get '/api/v2/storefront/products?page=1&per_page=10' }
it 'returns the default number of products' do
- expect(json_response['data'].count).to eq 6
+ expect(json_response['data'].count).to eq 7
end
end
@@ -227,7 +243,7 @@ describe 'API V2 Storefront Products Spec', type: :request do
before { get '/api/v2/storefront/products?page=1&per_page=-1' }
it 'returns the default number of products' do
- expect(json_response['data'].count).to eq 6
+ expect(json_response['data'].count).to eq 7
end
end
@@ -235,7 +251,7 @@ describe 'API V2 Storefront Products Spec', type: :request do
before { get '/api/v2/storefront/products?page=1&per_page=0' }
it 'returns the default number of products' do
- expect(json_response['data'].count).to eq 6
+ expect(json_response['data'].count).to eq 7
end
end
end
diff --git a/core/app/finders/spree/products/find.rb b/core/app/finders/spree/products/find.rb
index 6c54da673d..b2d1332315 100644
--- a/core/app/finders/spree/products/find.rb
+++ b/core/app/finders/spree/products/find.rb
@@ -16,7 +16,7 @@ module Spree
@sort_by = params.dig(:sort_by)
@deleted = params.dig(:filter, :show_deleted)
@discontinued = params.dig(:filter, :show_discontinued)
- @properties = params.dig(:product_properties)
+ @properties = params.dig(:filter, :properties)
end
def execute
@@ -172,7 +172,7 @@ module Spree
def by_properties(products)
return products unless properties? && properties.values.reject(&:empty?).present?
- product_ids = properties.map do |property_id, product_properties_values|
+ product_ids = properties.to_unsafe_hash.map do |property_id, product_properties_values|
next if product_properties_values.empty?
values = product_properties_values.split(',').map(&:strip)
diff --git a/core/lib/spree/core/search/base.rb b/core/lib/spree/core/search/base.rb
index 7a9ba70da1..e0c0f49785 100644
--- a/core/lib/spree/core/search/base.rb
+++ b/core/lib/spree/core/search/base.rb
@@ -42,10 +42,10 @@ module Spree
filter: {
price: price,
option_value_ids: option_value_ids,
+ properties: product_properties,
taxons: taxon&.id
},
- sort_by: sort_by,
- product_properties: product_properties
+ sort_by: sort_by
},
current_currency: current_currency
).execute
@@ -114,7 +114,7 @@ module Spree
end
def build_option_value_ids(params)
- filter_params = Spree::OptionType.all.map(&:filter_param)
+ filter_params = Spree::OptionType.filterable.map(&:filter_param)
filter_params.reduce([]) do |acc, filter_param|
acc + params[filter_param].to_s.split(',')
@@ -137,7 +137,7 @@ module Spree
else
1
end
- @properties[:product_properties] = params[:properties].try(:to_unsafe_hash)
+ @properties[:product_properties] = params[:properties]
end
end
end
diff --git a/frontend/app/views/spree/products/_filters_mobile.html.erb b/frontend/app/views/spree/products/_filters_mobile.html.erb
index ff4ebb34a8..23ac3c74d7 100644
--- a/frontend/app/views/spree/products/_filters_mobile.html.erb
+++ b/frontend/app/views/spree/products/_filters_mobile.html.erb
@@ -33,7 +33,7 @@
<% available_properties.each do |property| %>
<% if property.uniq_values.present? %>
<% property_name = property.name %>
- <% collapsed = params.dig(:product_properties, property.id.to_s).blank? %>
+ <% collapsed = params.dig(:properties, property.id.to_s).blank? %>
<div class="card plp-overlay-card">
<div class="card-header text-uppercase <%= 'collapsed' if collapsed %> plp-overlay-card-header" data-toggle="collapse" id="filtersMobile<%= property_name %>" data-target="#collapseFilterMobile<%= property_name %>" aria-expanded="true" aria-controls="collapseFilterMobile<%= property_name %>" role="heading" aria-level="3">
<%= property_name %>