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
allow filtering Products by giving a min price, a max price or both prices
Łukasz Adamczykde927f194e9c
diff --git a/core/app/finders/spree/products/find.rb b/core/app/finders/spree/products/find.rbindex 8e3ea8d041..b3bb71b937 100644--- a/core/app/finders/spree/products/find.rb+++ b/core/app/finders/spree/products/find.rb@@ -6,7 +6,7 @@ module Spree@ids = String(params.dig(:filter, :ids)).split(',')@skus = String(params.dig(:filter, :skus)).split(',')- @price = String(params.dig(:filter, :price)).split(',').map(&:to_f)+ @price = map_prices(String(params.dig(:filter, :price)).split(','))@currency = current_currency@taxons = taxon_ids(params.dig(:filter, :taxons))@concat_taxons = taxon_ids(params.dig(:filter, :concat_taxons))@@ -228,6 +228,12 @@ module Spreediscontinued ? products : products.availableend+ def map_prices(prices)+ prices.map do |price|+ price == 'Infinity' ? Float::INFINITY : price.to_f+ end+ end+def taxon_ids(taxons_ids)return if taxons_ids.nil? || taxons_ids.to_s.blank?diff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex 6242b9e844..41f80212f0 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -1005,6 +1005,7 @@ en:match_choices:all: Allnone: None+ max: 'Max'max_items: Max Itemsmember_since: Member Sincememo: Memo@@ -1013,6 +1014,7 @@ en:meta_title: Meta Titlemetadata: Metadatamutable: Mutable+ min: 'Min'minimal_amount: Minimal Amountmissing_return_authorization: ! 'Missing Return Authorization for %{item_name}.'month: Month@@ -1247,8 +1249,6 @@ en:done: DONEdefault: DEFAULTfilter_by: FILTER BY- max_price: 'Max'- min_price: 'Min'newest_first: NEWEST FIRSTno_results: No resultsno_results_found: No results founddiff --git a/core/lib/spree/core/search/base.rb b/core/lib/spree/core/search/base.rbindex e0c0f49785..dd137b293d 100644--- a/core/lib/spree/core/search/base.rb+++ b/core/lib/spree/core/search/base.rb@@ -108,6 +108,7 @@ module Spreehigh_price = Monetize.parse(price_param.remove("#{less_than_string} ")).to_ielselow_price, high_price = Monetize.parse_collection(price_param).map(&:to_i)+ high_price = Float::INFINITY if high_price.zero?end"#{low_price},#{high_price}"diff --git a/frontend/app/assets/javascripts/spree/frontend/views/spree/products/index.js b/frontend/app/assets/javascripts/spree/frontend/views/spree/products/index.jsindex dee00eb013..dabf3d9b2a 100644--- a/frontend/app/assets/javascripts/spree/frontend/views/spree/products/index.js+++ b/frontend/app/assets/javascripts/spree/frontend/views/spree/products/index.js@@ -109,23 +109,17 @@ Spree.ready(function ($) {}});- $('#filterPriceRange').on('submit', function(event) {- event.preventDefault();-+ $('#filterPriceRange').submit(function(event) {var $priceRangeForm = $(event.currentTarget);var minPrice = parseInt($priceRangeForm.find('[name="min_price"]').val());var maxPrice = parseInt($priceRangeForm.find('[name="max_price"]').val());- var searchParams = new URLSearchParams(location.search);- var priceRange = [- SPREE_CURRENCY_SYMBOL,- minPrice,- ' - ',- SPREE_CURRENCY_SYMBOL,- maxPrice- ]-- searchParams.set('price', priceRange.join(''))- setNewUrl(searchParams);+ var formattedPriceRange = [+ minPrice || 0,+ '-',+ maxPrice || 0+ ].join(' ');++ $priceRangeForm.find('[name="price"]').val(formattedPriceRange)});});diff --git a/frontend/app/views/spree/products/_filters_desktop.html.erb b/frontend/app/views/spree/products/_filters_desktop.html.erbindex 1c2d2442b9..c6ae23f722 100644--- a/frontend/app/views/spree/products/_filters_desktop.html.erb+++ b/frontend/app/views/spree/products/_filters_desktop.html.erb@@ -84,9 +84,10 @@<% end %><% end %>- <%= form_with id: 'filterPriceRange', method: :get do |f| %>- <%= f.number_field :min_price, min: 0, step: 1, placeholder: Spree.t('plp.min_price') %>- <%= f.number_field :max_price, min: 0, step: 1, placeholder: Spree.t('plp.max_price') %>+ <%= form_with url: permitted_params, id: 'filterPriceRange', method: :get do |f| %>+ <%= f.hidden_field :price %>+ <%= f.number_field :min_price, value: params[:min_price], min: 0, step: 1, placeholder: Spree.t(:min) %>+ <%= f.number_field :max_price, value: params[:max_price], min: 0, step: 1, placeholder: Spree.t(:max) %><%= f.submit Spree.t(:search) %><% end %>diff --git a/frontend/app/views/spree/shared/_locale_and_currency.html.erb b/frontend/app/views/spree/shared/_locale_and_currency.html.erbindex 827a3f0955..9e2698801f 100644--- a/frontend/app/views/spree/shared/_locale_and_currency.html.erb+++ b/frontend/app/views/spree/shared/_locale_and_currency.html.erb@@ -1,7 +1,6 @@<script>var SPREE_LOCALE = '<%= I18n.locale %>'var SPREE_CURRENCY = '<%= current_currency %>'- var SPREE_CURRENCY_SYMBOL = '<%= currency_symbol(current_currency) %>'var SPREE_DEFAULT_CURRENCY = '<%= current_store.default_currency %>'var SPREE_DEFAULT_LOCALE = '<%= current_store.default_locale %>'</script>diff --git a/frontend/spec/features/products_spec.rb b/frontend/spec/features/products_spec.rbindex 4206e7233d..bb1a66bb63 100644--- a/frontend/spec/features/products_spec.rb+++ b/frontend/spec/features/products_spec.rb@@ -291,24 +291,64 @@ describe 'Visiting Products', type: :feature, inaccessible: true do'Ruby on Rails Stein'])end- it 'is able to display products based on a given price range', js: true do- within(:css, '.plp-filters-scroller') do- click_on Spree.t('plp.price')+ context 'filtering by giving a price range', js: true do+ it 'is able to display products between 55 and 103 dollars' do+ within(:css, '.plp-filters-scroller') do+ click_on Spree.t('plp.price')++ fill_in Spree.t(:min), with: '55'+ fill_in Spree.t(:max), with: '103'+ click_on Spree.t(:search)+ end++ expect(page).to have_css('.product-component-name').exactly(3).times++ product_names = page.all('.product-component-name').map(&:text).flatten.compact+ expect(product_names).to contain_exactly(+ 'Ruby on Rails Bag',+ 'Ruby on Rails Mug',+ 'Ruby on Rails Tote'+ )+ end++ it 'is able to display products when providing only min amount of 55 dollars' do+ within(:css, '.plp-filters-scroller') do+ click_on Spree.t('plp.price')- fill_in Spree.t('plp.min_price'), with: '55'- fill_in Spree.t('plp.max_price'), with: '103'+ fill_in Spree.t(:min), with: '56'+ click_on Spree.t(:search)+ end++ expect(page).to have_css('.product-component-name').exactly(7).times- click_on Spree.t(:search)+ product_names = page.all('.product-component-name').map(&:text).flatten.compact+ expect(product_names).to contain_exactly(+ 'Ruby on Rails Ringer T-Shirt',+ 'Ruby on Rails Bag',+ 'Ruby on Rails Baseball Jersey',+ 'Ruby on Rails Stein',+ 'Ruby on Rails Jr. Spaghetti',+ 'Ruby Baseball Jersey',+ 'Apache Baseball Jersey'+ )end- expect(page).to have_css('.product-component-name').exactly(3).times+ it 'is able to display products when providing only max amount of 56 dollars' do+ within(:css, '.plp-filters-scroller') do+ click_on Spree.t('plp.price')++ fill_in Spree.t(:max), with: '56'+ click_on Spree.t(:search)+ end++ expect(page).to have_css('.product-component-name').exactly(2).times- product_names = page.all('.product-component-name').map(&:text).flatten.compact.delete('')- expect(product_names.sort).to contain_exactly(- 'Ruby on Rails Bag',- 'Ruby on Rails Mug',- 'Ruby on Rails Tote'- )+ product_names = page.all('.product-component-name').map(&:text).flatten.compact+ expect(product_names).to contain_exactly(+ 'Ruby on Rails Mug',+ 'Ruby on Rails Tote'+ )+ endendcontext 'pagination' do