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
[SD-987] Add filtering products by properties
Robert Nowakowski23e46cfeefd0
diff --git a/backend/app/views/spree/admin/properties/_form.html.erb b/backend/app/views/spree/admin/properties/_form.html.erbindex e6178839cf..3619319d68 100644--- a/backend/app/views/spree/admin/properties/_form.html.erb+++ b/backend/app/views/spree/admin/properties/_form.html.erb@@ -13,4 +13,10 @@<%= f.error_message_on :presentation %><% end %></div>+ <div class="col-12 col-md-6">+ <%= f.field_container :filterable, class: ['form-group'] do %>+ <%= f.label :filterable, Spree.t(:filterable) %>+ <%= f.check_box :filterable %>+ <% end %>+ </div></div>diff --git a/backend/app/views/spree/admin/properties/index.html.erb b/backend/app/views/spree/admin/properties/index.html.erbindex c1c769f8c7..57aca21a23 100644--- a/backend/app/views/spree/admin/properties/index.html.erb+++ b/backend/app/views/spree/admin/properties/index.html.erb@@ -43,6 +43,7 @@<tr data-hook="listing_properties_header"><th><%= Spree.t(:name) %></th><th><%= Spree.t(:presentation) %></th>+ <th><%= Spree.t(:filterable) %></th><th class="actions"></th></tr></thead>@@ -51,6 +52,7 @@<tr id="<%= spree_dom_id property %>" data-hook="listing_properties_row"><td><%= property.name %></td><td><%= property.presentation %></td>+ <td><%= property.filterable %></td><td class="actions"><span class="d-flex justify-content-end"><%= link_to_edit(property, no_text: true) if can?(:edit, property) %>diff --git a/backend/spec/features/admin/properties/edit_property_spec.rb b/backend/spec/features/admin/properties/edit_property_spec.rbnew file mode 100644index 0000000000..d130cdbe87--- /dev/null+++ b/backend/spec/features/admin/properties/edit_property_spec.rb@@ -0,0 +1,25 @@+require 'spec_helper'++describe 'Property edit spec', type: :feature, js: true do+ stub_authorization!++ let(:property) { create(:property, filterable: false) }++ context 'editing option type' do+ before do+ visit spree.edit_admin_property_path(property)+ end++ it 'allows to update filtering attribute of option type' do+ expect(page).to have_unchecked_field('property_filterable')++ check('property_filterable')+ click_button 'Update'++ expect(page).to have_content 'successfully updated!'++ within("#spree_property_#{property.id}") { click_icon(:edit) }+ expect(page).to have_checked_field('property_filterable')+ end+ end+enddiff --git a/core/app/finders/spree/products/find.rb b/core/app/finders/spree/products/find.rbindex b633d258bb..4b04fe52a0 100644--- a/core/app/finders/spree/products/find.rb+++ b/core/app/finders/spree/products/find.rb@@ -16,6 +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)enddef execute@@ -28,6 +29,7 @@ module Spreeproducts = by_name(products)products = by_options(products)products = by_option_value_ids(products)+ products = by_properties(products)products = include_deleted(products)products = include_discontinued(products)products = ordered(products)@@ -38,7 +40,7 @@ module Spreeprivateattr_reader :ids, :skus, :price, :currency, :taxons, :concat_taxons, :name, :options,- :option_value_ids, :scope, :sort_by, :deleted, :discontinued+ :option_value_ids, :scope, :sort_by, :deleted, :discontinued, :propertiesdef ids?ids.present?@@ -80,6 +82,10 @@ module Spreesort_by.present?end+ def properties?+ properties.present?+ end+def name_matcherSpree::Product.arel_table[:name].matches("%#{name}%")end@@ -163,6 +169,15 @@ module Spreeproducts.where(id: product_ids)end+ def by_properties(products)+ return products unless properties?++ products.+ joins(:product_properties).+ where(spree_product_properties: { value: properties.values }).+ distinct+ end+def option_types_count(option_value_ids)Spree::OptionValue.where(id: option_value_ids).diff --git a/core/app/models/spree/property.rb b/core/app/models/spree/property.rbindex 650a3e1c86..82cb3b8567 100644--- a/core/app/models/spree/property.rb+++ b/core/app/models/spree/property.rb@@ -9,11 +9,16 @@ module Spreevalidates :name, :presentation, presence: truescope :sorted, -> { order(:name) }+ scope :filterable, -> { where(filterable: true) }after_touch :touch_all_productsself.whitelisted_ransackable_attributes = ['presentation']+ def uniq_values+ product_properties.pluck(:value).compact.uniq+ end+privatedef touch_all_productsdiff --git a/core/db/migrate/20201023152810_add_filterable_to_spree_properties.rb b/core/db/migrate/20201023152810_add_filterable_to_spree_properties.rbnew file mode 100644index 0000000000..e09ede30ae--- /dev/null+++ b/core/db/migrate/20201023152810_add_filterable_to_spree_properties.rb@@ -0,0 +1,8 @@+class AddFilterableToSpreeProperties < ActiveRecord::Migration[6.0]+ def change+ unless column_exists?(:spree_properties, :filterable)+ add_column :spree_properties, :filterable, :boolean, default: false, null: false+ add_index :spree_properties, :filterable+ end+ end+enddiff --git a/core/lib/spree/core/search/base.rb b/core/lib/spree/core/search/base.rbindex 87614fa1fe..8e8e7833cf 100644--- a/core/lib/spree/core/search/base.rb+++ b/core/lib/spree/core/search/base.rb@@ -44,7 +44,8 @@ module Spreeoption_value_ids: option_value_ids,taxons: taxon&.id},- sort_by: sort_by+ sort_by: sort_by,+ product_properties: product_properties},current_currency: current_currency).execute@@ -120,6 +121,10 @@ module Spreeendend+ def build_product_properties(params)+ params.slice(*Spree::Property.pluck(:name))+ end+def prepare(params)@properties[:taxon] = params[:taxon].blank? ? nil : Spree::Taxon.find(params[:taxon])@properties[:keywords] = params[:keywords]@@ -136,6 +141,7 @@ module Spreeelse1end+ @properties[:product_properties] = build_product_properties(params)endendenddiff --git a/frontend/app/helpers/spree/frontend_helper.rb b/frontend/app/helpers/spree/frontend_helper.rbindex b88e0a6e80..669613c228 100644--- a/frontend/app/helpers/spree/frontend_helper.rb+++ b/frontend/app/helpers/spree/frontend_helper.rb@@ -246,7 +246,9 @@ module Spreeenddef filtering_params- @filtering_params ||= available_option_types.map(&:filter_param).concat(static_filters)+ @filtering_params ||= available_option_types.map(&:filter_param).+ concat(available_properties.map { |property| property.name.split.join('-') }).+ concat(static_filters)enddef filtering_params_cache_key@@ -264,6 +266,17 @@ module Spree@available_option_typesend+ def available_properties_cache_key+ @available_properties_cache_key ||= Spree::Property.filterable.maximum(:updated_at)&.utc&.to_i+ end++ def available_properties+ @available_properties ||= Rails.cache.fetch("available-properties/#{available_properties_cache_key}") do+ Spree::Property.includes(:product_properties).filterable.to_a+ end+ @available_properties+ end+def spree_social_link(service)return '' if current_store.send(service).blank?diff --git a/frontend/app/views/spree/products/_filters_desktop.html.erb b/frontend/app/views/spree/products/_filters_desktop.html.erbindex 6b7025d2eb..8b98f7e2a4 100644--- a/frontend/app/views/spree/products/_filters_desktop.html.erb+++ b/frontend/app/views/spree/products/_filters_desktop.html.erb@@ -29,6 +29,36 @@</div></div><% end %>+ <% available_properties.each do |property| %>+ <% if property.uniq_values.present? %>+ <div class="w-100 card plp-filters-card">+ <% property_name = property.name %>+ <% collapsed = params[property_name].blank? %>++ <button class="card-header <%= 'collapsed' if collapsed %> px-1 plp-filters-card-header" id="filters<%= property_name %>" data-toggle="collapse" data-target="#collapseFilter<%= property_name %>" aria-expanded="<%= !collapsed %>" aria-controls="collapseFilter<%= property_name %>" role="heading" aria-level="2">+ <%= property.presentation.titleize %>+ <%= icon(name: 'plus',+ classes: 'mt-1 float-right arrow plp-filters-card-header-arrow--plus',+ width: 16,+ height: 16) %>+ <%= icon(name: 'minus',+ classes: 'mt-1 float-right arrow plp-filters-card-header-arrow--minus',+ width: 16,+ height: 16) %>+ </button>+ <div id="collapseFilter<%= property_name %>" class="collapse <%= 'show' unless collapsed %>" aria-labelledby="filters<%= property_name %>">+ <div class="card-body px-0 pt-1">+ <%= render 'spree/shared/properties',+ params_property_name: params[property_name],+ property: property,+ permitted_params: permitted_params,+ property_name: property_name+ %>+ </div>+ </div>+ </div>+ <% end %>+ <% end %><% price_collapsed = params[:price].blank? %><div class="w-100 card plp-filters-card"><button class="card-header text-capitalize <%= 'collapsed' if price_collapsed %> px-1 plp-filters-card-header" id="filtersPrice" data-toggle="collapse" data-target="#collapseFilterPrice" aria-expanded="<% !price_collapsed %>" aria-controls="collapseFilterPrice" role="heading" aria-level="2">diff --git a/frontend/app/views/spree/shared/_properties.html.erb b/frontend/app/views/spree/shared/_properties.html.erbnew file mode 100644index 0000000000..b4a73e2127--- /dev/null+++ b/frontend/app/views/spree/shared/_properties.html.erb@@ -0,0 +1,12 @@+<% cache base_cache_key + [filtering_params_cache_key, @taxon&.id] do %>+ <% selected_properties = params[property_name]&.split(',') || [] %>+ <% property.uniq_values.each do |value| %>+ <% value_slug = CGI.escape(value) %>+ <% property_value_param = (selected_properties.include?(value_slug) ? selected_properties - [value_slug] : selected_properties + [value_slug]).join(',') %>+ <%= link_to permitted_params.merge(property.name => property_value_param, menu_open: 1), data: { params: permitted_params.merge(property.name => property_value_param, menu_open: 1), id: value_slug, filter_name: property.name, multiselect: true } do %>+ <div class="d-inline-block text-uppercase py-1 px-2 m-1 plp-overlay-card-item <%= 'plp-overlay-card-item--selected' if selected_properties.include?(value_slug) %>">+ <%= value %>+ </div>+ <% end %>+ <% end %>+<% end %>diff --git a/frontend/spec/features/products_filtering_spec.rb b/frontend/spec/features/products_filtering_spec.rbindex 0d06d02987..7626309e16 100644--- a/frontend/spec/features/products_filtering_spec.rb+++ b/frontend/spec/features/products_filtering_spec.rb@@ -10,6 +10,14 @@ describe 'Products filtering', :js, :caching dolet!(:option_type_2) { create :option_type, name: 'color', presentation: 'Color' }let!(:option_value_2_1) { create :option_value, option_type: option_type_2, name: 'green', presentation: 'Green' }+ let!(:property_1) { create :property, name: 'manufacturer', presentation: 'Manufacturer', filterable: true }+ let!(:product_property_1) { create :product_property, value: 'Wilson', property: property_1 }++ let!(:property_2) { create :property, name: 'brand', presentation: 'Brand', filterable: true }+ let!(:product_property_2) { create :product_property, value: 'Zeta', property: property_2 }++ let!(:property_3) { create :property, name: 'collection', presentation: 'Collection', filterable: true }+let!(:product_1) { create :product, name: 'First shirt', option_types: [option_type_1] }let!(:variant_1_1) { create :variant, product: product_1, option_values: [option_value_1_1] }@@ -73,4 +81,27 @@ describe 'Products filtering', :js, :caching doexpect(filters).not_to have_content('Color')endend++ context 'property filters' do+ it 'displays filterable properties' do+ visit spree.nested_taxons_path(taxon)++ %w[Manufacturer Brand].each do |property_name|+ expect(filters).to have_content(property_name)+ end+ end++ it 'does not display unfilterable properties' do+ property_2.update!(filterable: false)+ visit spree.nested_taxons_path(taxon)++ expect(filters).not_to have_content('Brand')+ end++ it 'does not display properties that do not have values' do+ visit spree.nested_taxons_path(taxon)++ expect(filters).not_to have_content('Collection')+ end+ endend