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: computed the product filter price range from base prices, respecting hidden prices

Damian Legawiecf6c5b9ce8710
diff --git a/spree/api/app/controllers/spree/api/v3/store/products/filters_controller.rb b/spree/api/app/controllers/spree/api/v3/store/products/filters_controller.rb
index 6875f91e37..82639f0317 100644
--- a/spree/api/app/controllers/spree/api/v3/store/products/filters_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/products/filters_controller.rb
@@ -40,6 +40,7 @@ module Spree
current_store.id,
current_currency,
current_locale,
+ (hide_prices? ? 'prices_hidden' : nil),
category&.cache_key_with_version,
collection&.cache_key_with_version,
search_query,
diff --git a/spree/api/app/serializers/spree/api/v3/product_filters_serializer.rb b/spree/api/app/serializers/spree/api/v3/product_filters_serializer.rb
index 41bef6ec9a..7d09758f0e 100644
--- a/spree/api/app/serializers/spree/api/v3/product_filters_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/product_filters_serializer.rb
@@ -12,6 +12,9 @@ module Spree
attribute :filters do |result|
result.filters.filter_map do |filter|
filter_type = filter[:type]
+ # A price range is prices; a gated guest sees none.
+ next if filter_type == 'price_range' && params[:hide_prices]
+
serializer_class = case filter_type
when 'price_range'
Spree.api.product_filter_price_range_serializer
diff --git a/spree/api/app/services/spree/api/v3/filters_aggregator.rb b/spree/api/app/services/spree/api/v3/filters_aggregator.rb
index bdbe2b8794..3237f2cfd6 100644
--- a/spree/api/app/services/spree/api/v3/filters_aggregator.rb
+++ b/spree/api/app/services/spree/api/v3/filters_aggregator.rb
@@ -51,8 +51,11 @@ module Spree
def price_filter
- # Remove ordering to avoid PostgreSQL DISTINCT + ORDER BY conflicts
- prices = Spree::Price.for_products(@scope.reorder(''), @currency)
+ # Remove ordering to avoid PostgreSQL DISTINCT + ORDER BY conflicts.
+ # Base prices only: a price list's rows are for its own audience
+ # (a company's contract, a draft or scheduled list), and the range is
+ # shown to everyone.
+ prices = Spree::Price.base_prices.for_products(@scope.reorder(''), @currency)
min = prices.minimum(:amount)
max = prices.maximum(:amount)
return nil if min.nil? || max.nil?
diff --git a/spree/api/spec/controllers/spree/api/v3/store/products/filters_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/products/filters_controller_spec.rb
index 3ef391733b..bced5159ec 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/products/filters_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/products/filters_controller_spec.rb
@@ -64,6 +64,28 @@ RSpec.describe Spree::Api::V3::Store::Products::FiltersController, type: :contro
expect(price_filter).to have_key('currency')
end
+ context 'on a channel that hides prices from guests' do
+ before do
+ store.default_channel.update!(preferred_storefront_access: 'prices_hidden')
+ request.headers['X-Spree-Channel'] = store.default_channel.code
+ end
+
+ it 'leaves out the price range for a guest' do
+ get :index
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['filters'].pluck('type')).not_to include('price_range')
+ end
+
+ it 'shows the price range to a signed-in customer' do
+ request.headers['Authorization'] = "Bearer #{jwt_token}"
+
+ get :index
+
+ expect(json_response['filters'].pluck('type')).to include('price_range')
+ end
+ end
+
it 'returns availability filter' do
get :index
diff --git a/spree/api/spec/services/spree/api/v3/filters_aggregator_spec.rb b/spree/api/spec/services/spree/api/v3/filters_aggregator_spec.rb
index 04ece5e2e3..e3887b372c 100644
--- a/spree/api/spec/services/spree/api/v3/filters_aggregator_spec.rb
+++ b/spree/api/spec/services/spree/api/v3/filters_aggregator_spec.rb
@@ -81,6 +81,18 @@ RSpec.describe Spree::Api::V3::FiltersAggregator do
expect(price_filter[:max]).to be_a(Numeric)
expect(price_filter[:currency]).to eq(currency)
end
+
+ # The range is public; a price list's rows belong to its own audience.
+ it 'leaves price-list prices out of the range' do
+ price_list = create(:price_list, store: store)
+ create(:price, variant: product1.master, price_list: price_list, amount: 0.5, currency: currency)
+ create(:price, variant: product2.master, price_list: price_list, amount: 9_999, currency: currency)
+
+ price_filter = result[:filters].find { |f| f[:type] == 'price_range' }
+
+ expect(price_filter[:min]).to be > 0.5
+ expect(price_filter[:max]).to be < 9_999
+ end
end
describe 'availability filter' do