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.

WIP: price range form for filtering

Łukasz Adamczyk4581fd061204
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index a84da6c25c..6242b9e844 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -1247,6 +1247,8 @@ en:
done: DONE
default: DEFAULT
filter_by: FILTER BY
+ max_price: 'Max'
+ min_price: 'Min'
newest_first: NEWEST FIRST
no_results: No results
no_results_found: No results found
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.js
index 169c4bb707..dee00eb013 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
@@ -108,4 +108,24 @@ Spree.ready(function ($) {
setNewUrl(searchParams);
}
});
+
+ $('#filterPriceRange').on('submit', function(event) {
+ event.preventDefault();
+
+ 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);
+ });
});
diff --git a/frontend/app/views/spree/products/_filters_desktop.html.erb b/frontend/app/views/spree/products/_filters_desktop.html.erb
index a60f612c09..1c2d2442b9 100644
--- a/frontend/app/views/spree/products/_filters_desktop.html.erb
+++ b/frontend/app/views/spree/products/_filters_desktop.html.erb
@@ -83,6 +83,13 @@
</div>
<% 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') %>
+
+ <%= f.submit Spree.t(:search) %>
+ <% end %>
</div>
</div>
</div>
diff --git a/frontend/app/views/spree/shared/_locale_and_currency.html.erb b/frontend/app/views/spree/shared/_locale_and_currency.html.erb
index 9e2698801f..827a3f0955 100644
--- a/frontend/app/views/spree/shared/_locale_and_currency.html.erb
+++ b/frontend/app/views/spree/shared/_locale_and_currency.html.erb
@@ -1,6 +1,7 @@
<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.rb
index d810653e0d..4206e7233d 100644
--- a/frontend/spec/features/products_spec.rb
+++ b/frontend/spec/features/products_spec.rb
@@ -291,6 +291,26 @@ 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')
+
+ fill_in Spree.t('plp.min_price'), with: '55'
+ fill_in Spree.t('plp.max_price'), 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.delete('')
+ expect(product_names.sort).to contain_exactly(
+ 'Ruby on Rails Bag',
+ 'Ruby on Rails Mug',
+ 'Ruby on Rails Tote'
+ )
+ end
+
context 'pagination' do
before { Spree::Config.products_per_page = 3 }