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.

allow clearing all Filters on PLP

Łukasz Adamczykb63cea705fe4
diff --git a/frontend/app/helpers/spree/products_filters_helper.rb b/frontend/app/helpers/spree/products_filters_helper.rb
index f00b518bc4..1ef51f181e 100644
--- a/frontend/app/helpers/spree/products_filters_helper.rb
+++ b/frontend/app/helpers/spree/products_filters_helper.rb
@@ -1,8 +1,12 @@
module Spree
module ProductsFiltersHelper
+ PRICE_FILTER_NAME = 'price'.freeze
+
FILTER_LINK_CSS_CLASSES = 'd-inline-block text-uppercase py-1 px-2 m-1 plp-overlay-card-item'.freeze
ACTIVE_FILTER_LINK_CSS_CLASSES = 'plp-overlay-card-item--selected'.freeze
+ CLEAR_ALL_FILTERS_LINK_CSS_CLASSES = 'btn spree-btn btn-outline-primary w-100 mb-4'.freeze
+
def price_filters
@price_filters ||= [
filters_less_than_price_range(50),
@@ -90,12 +94,37 @@ module Spree
opts[:is_selected] ||= is_selected
content_tag :div do
- base_filter_link(url, price_range, opts.merge(is_selected: is_selected, filter_name: 'price', id: price_param))
+ base_filter_link(url, price_range, opts.merge(is_selected: is_selected, filter_name: PRICE_FILTER_NAME, id: price_param))
end
end
+ def product_filters_present?(permitted_params)
+ properties = permitted_params.fetch(:properties, {}).to_unsafe_h
+ flatten_params = permitted_params.to_h.merge(properties)
+
+ flatten_params.any? { |name, value| product_filter_present?(name, value) }
+ end
+
+ def clear_all_filters_link(permitted_params, opts = {})
+ opts[:css_class] ||= CLEAR_ALL_FILTERS_LINK_CSS_CLASSES
+ sort_by_param = permitted_params.slice(:sort_by)
+
+ link_to Spree.t('plp.clear_all'), sort_by_param, class: opts[:css_class], data: { params: sort_by_param }
+ end
+
private
+ def product_filter_present?(filter_param, filter_value)
+ filter_param.in?(product_filters_params) && filter_value.present?
+ end
+
+ def product_filters_params
+ options_filter_params = OptionType.filterable.map(&:filter_param)
+ properties_filter_params = Property.filterable.pluck(:filter_param)
+
+ options_filter_params + properties_filter_params + [PRICE_FILTER_NAME]
+ end
+
def base_filter_link(url, label, opts = {})
opts[:params] ||= url
diff --git a/frontend/app/views/spree/products/_filters_desktop.html.erb b/frontend/app/views/spree/products/_filters_desktop.html.erb
index d7259ac914..e1a0c7c5e0 100644
--- a/frontend/app/views/spree/products/_filters_desktop.html.erb
+++ b/frontend/app/views/spree/products/_filters_desktop.html.erb
@@ -1,5 +1,7 @@
<% cache filters_cache_key('desktop') do %>
<div id="plp-filters-accordion" class="d-none d-lg-block col-lg-3 pr-5 position-sticky h-100 plp-filters" data-hook="taxon_sidebar_navigation">
+ <%= clear_all_filters_link(permitted_params) if product_filters_present?(permitted_params) %>
+
<div class="plp-filters-scroller">
<% @available_option_types.each do |option_type| %>
<div class="w-100 card plp-filters-card">
diff --git a/frontend/app/views/spree/products/_filters_mobile.html.erb b/frontend/app/views/spree/products/_filters_mobile.html.erb
index 4a2c927b6b..943a35271e 100644
--- a/frontend/app/views/spree/products/_filters_mobile.html.erb
+++ b/frontend/app/views/spree/products/_filters_mobile.html.erb
@@ -88,7 +88,7 @@
<% end %>
<div class="container position-absolute text-center plp-overlay-buttons pt-3">
- <%= link_to Spree.t('plp.clear_all'), permitted_params.select { |key, value| key == "sort_by"}, class: 'btn spree-btn btn-outline-primary w-100 mb-4', data: { params: permitted_params.select { |key, value| key == "sort_by"} } %>
+ <%= clear_all_filters_link(permitted_params) %>
<%= link_to Spree.t('plp.done'), permitted_params, id: 'filterProductsButtonMobile', class: 'btn btn-primary spree-btn w-100 done-btn', data: { params: permitted_params } %>
</div>
</div>
diff --git a/frontend/spec/features/products_filtering_spec.rb b/frontend/spec/features/products_filtering_spec.rb
index d32999d6eb..e835eece96 100644
--- a/frontend/spec/features/products_filtering_spec.rb
+++ b/frontend/spec/features/products_filtering_spec.rb
@@ -47,6 +47,13 @@ describe 'Products filtering', :js do
have_css '.plp-overlay-card-item--selected', text: value
end
+ def expect_working_filters_clearing
+ click_on 'CLEAR ALL'
+ expect(page).to have_content 'First shirt'
+ expect(page).to have_content 'Second shirt'
+ expect(page).not_to have_css('.plp-overlay-card-item--selected')
+ end
+
def filters
find('#plp-filters-accordion')
end
@@ -54,6 +61,8 @@ describe 'Products filtering', :js do
it 'correctly filters Products' do
visit spree.nested_taxons_path(taxon)
+ expect(page).not_to have_content('CLEAR ALL')
+
search_by 'shirt'
expect(page).to have_content 'First shirt'
@@ -65,6 +74,21 @@ describe 'Products filtering', :js do
expect(page).to have_content 'First shirt'
expect(page).to have_selected_filter_with(value: 'M')
expect(page).to have_selected_filter_with(value: 'S')
+
+ expect_working_filters_clearing
+
+ click_on_filter 'Brand', value: 'Alpha'
+ expect(page).not_to have_content 'First shirt'
+ expect(page).to have_content 'Second shirt'
+ expect(page).to have_selected_filter_with(value: 'ALPHA')
+
+ expect_working_filters_clearing
+
+ click_on_filter 'Price', value: '$50 - $100'
+ expect(page).to have_content 'No results'
+
+ expect_working_filters_clearing
+
expect(current_path).to eq spree.products_path
end