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.

adjust caching for Properties and Options Filters on PLP

Łukasz Adamczyka5be659eaf7c
diff --git a/frontend/app/helpers/spree/frontend_helper.rb b/frontend/app/helpers/spree/frontend_helper.rb
index efda927fa0..939c01256e 100644
--- a/frontend/app/helpers/spree/frontend_helper.rb
+++ b/frontend/app/helpers/spree/frontend_helper.rb
@@ -294,7 +294,10 @@ module Spree
end
def available_option_types_cache_key
- @available_option_types_cache_key ||= Spree::OptionType.filterable.maximum(:updated_at)&.utc&.to_i
+ @available_option_types_cache_key ||= [
+ Spree::OptionType.filterable.maximum(:updated_at).to_i,
+ products_for_filters_cache_key
+ ].join('/')
end
def available_option_types
@@ -305,7 +308,10 @@ module Spree
end
def available_properties_cache_key
- @available_properties_cache_key ||= Spree::Property.filterable.maximum(:updated_at)&.utc&.to_i
+ @available_properties_cache_key ||= [
+ Spree::Property.filterable.maximum(:updated_at).to_i,
+ products_for_filters_cache_key
+ ].join('/')
end
def available_properties
@@ -368,5 +374,13 @@ module Spree
def products_for_filters
Product.for_filters(current_currency, @taxon)
end
+
+ def products_for_filters_cache_key
+ [
+ products_for_filters.maximum(:updated_at).to_i,
+ current_currency,
+ @taxon&.permalink
+ ].compact.join('/')
+ end
end
end
diff --git a/frontend/spec/features/products_filtering_spec.rb b/frontend/spec/features/products_filtering_spec.rb
index d4f5edf260..6ed858718d 100644
--- a/frontend/spec/features/products_filtering_spec.rb
+++ b/frontend/spec/features/products_filtering_spec.rb
@@ -3,27 +3,36 @@ require 'spec_helper'
describe 'Products filtering', :js do
let!(:taxon) { create :taxon }
- let!(:option_type_1) { create :option_type, name: 'size', presentation: 'Size' }
- let!(:option_value_1_1) { create :option_value, option_type: option_type_1, name: 's', presentation: 'S' }
- let!(:option_value_1_2) { create :option_value, option_type: option_type_1, name: 'm', presentation: 'M' }
+ let!(:size) { create :option_type, name: 'size', presentation: 'Size' }
+ let!(:s_size) { create :option_value, option_type: size, name: 's', presentation: 'S' }
+ let!(:m_size) { create :option_value, option_type: size, name: 'm', presentation: 'M' }
- let!(: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!(:color) { create :option_type, name: 'color', presentation: 'Color' }
+ let!(:green_color) { create :option_value, option_type: color, 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!(:manufacturer) { create :property, name: 'manufacturer', presentation: 'Manufacturer', filterable: true }
+ let!(:wilson_manufacturer) { create :product_property, value: 'Wilson', property: manufacturer }
- let!(:property_2) { create :property, name: 'brand', presentation: 'Brand', filterable: true }
- let!(:product_property_2) { create :product_property, value: 'Zeta', property: property_2 }
- let!(:product_property_3) { create :product_property, value: 'Alpha', property: property_2 }
+ let!(:brand) { create :property, name: 'brand', presentation: 'Brand', filterable: true }
+ let!(:zeta_brand) { create :product_property, value: 'Zeta', property: brand }
+ let!(:alpha_brand) { create :product_property, value: 'Alpha', property: brand }
let!(:property_3) { create :property, name: 'collection', presentation: 'Collection', filterable: true }
- let!(:product_1) { create :product, name: 'First shirt', option_types: [option_type_1, option_type_2], product_properties: [product_property_2], taxons: [taxon] }
- let!(:variant_1_1) { create :variant, product: product_1, option_values: [option_value_1_1, option_value_2_1] }
+ let!(:product_1) { create :product, name: 'First shirt', option_types: [size, color], product_properties: [zeta_brand], taxons: [taxon] }
+ let!(:variant_1_1) { create :variant, product: product_1, option_values: [s_size, green_color] }
- let!(:product_2) { create :product, name: 'Second shirt', option_types: [option_type_1], product_properties: [product_property_1, product_property_3], taxons: [taxon] }
- let!(:variant_2_1) { create :variant, product: product_2, option_values: [option_value_1_2] }
+ let!(:product_2) { create :product, name: 'Second shirt', option_types: [size], product_properties: [wilson_manufacturer, alpha_brand], taxons: [taxon] }
+ let!(:variant_2_1) { create :variant, product: product_2, option_values: [m_size] }
+
+ def visit_taxons_page(taxon)
+ visit spree.nested_taxons_path(taxon)
+ end
+
+ def change_currency_to(currency)
+ find('.internationalization-options').click
+ select currency, from: 'switch_to_currency'
+ end
def search_by(text)
find('.search-icons').click
@@ -48,6 +57,10 @@ describe 'Products filtering', :js do
have_css '.plp-overlay-card-item--selected', text: value
end
+ def have_filter_with(value:)
+ have_css '.plp-overlay-card-item', text: value.upcase
+ end
+
def expect_working_filters_clearing
click_on 'CLEAR ALL'
expect(page).to have_content 'First shirt'
@@ -116,7 +129,7 @@ describe 'Products filtering', :js do
end
it 'does not display unfilterable option types' do
- option_type_2.update!(filterable: false)
+ color.update!(filterable: false)
visit spree.nested_taxons_path(taxon)
expect(filters).not_to have_content('Color')
@@ -133,7 +146,7 @@ describe 'Products filtering', :js do
end
it 'does not display unfilterable properties' do
- property_2.update!(filterable: false)
+ brand.update!(filterable: false)
visit spree.nested_taxons_path(taxon)
expect(filters).not_to have_content('Brand')
@@ -154,4 +167,201 @@ describe 'Products filtering', :js do
expect(page).to have_content('Second shirt')
end
end
+
+ context 'with cached filters' do
+ context 'when after visiting products page new filters were added' do
+ let(:jersey_manufacturer) { create(:product_property, value: 'Jerseys', property: manufacturer) }
+ let(:beta_brand) { create(:product_property, value: 'Beta', property: brand) }
+
+ let(:xl_size) { create(:option_value, option_type: size, name: 'xl', presentation: 'XL') }
+
+ it 'correctly displays filterable properties' do
+ visit_taxons_page(taxon)
+
+ click_on 'Manufacturer'
+ expect(page).to have_filter_with(value: 'Wilson')
+
+ click_on 'Brand'
+ expect(page).to have_filter_with(value: 'Zeta')
+ expect(page).to have_filter_with(value: 'Alpha')
+
+ product_1.product_properties << jersey_manufacturer
+ product_2.product_properties << beta_brand
+
+ visit_taxons_page(taxon)
+
+ click_on 'Manufacturer'
+ expect(page).to have_filter_with(value: 'Wilson')
+ expect(page).to have_filter_with(value: 'Jerseys')
+
+ click_on 'Brand'
+ expect(page).to have_filter_with(value: 'Zeta')
+ expect(page).to have_filter_with(value: 'Alpha')
+ expect(page).to have_filter_with(value: 'Beta')
+ end
+
+ it 'correctly displays filterable options' do
+ visit_taxons_page(taxon)
+
+ click_on 'Size'
+ expect(page).to have_filter_with(value: 'S')
+ expect(page).to have_filter_with(value: 'M')
+
+ product_1.variants << create(:variant, option_values: [xl_size])
+
+ visit_taxons_page(taxon)
+
+ click_on 'Size'
+ expect(page).to have_filter_with(value: 'S')
+ expect(page).to have_filter_with(value: 'M')
+ expect(page).to have_filter_with(value: 'XL')
+ end
+ end
+
+ context 'when switching between currencies' do
+ let(:eur) { 'EUR' }
+
+ let!(:gamma_brand) { create(:product_property, value: 'Gamma', property: brand) }
+ let!(:xl_size) { create(:option_value, option_type: size, name: 'xl', presentation: 'XL') }
+
+ let!(:product_3) { create(:product, name: '3rd shirt', product_properties: [gamma_brand], taxons: [taxon], currency: eur) }
+ let!(:variant_3) { create(:variant, product: product_3, currency: eur, option_values: [xl_size]) }
+
+ it 'correctly displays filterable properties' do
+ visit_taxons_page(taxon)
+
+ click_on 'Manufacturer'
+ expect(page).to have_filter_with(value: 'Wilson')
+
+ click_on 'Brand'
+ expect(page).to have_filter_with(value: 'Zeta')
+ expect(page).to have_filter_with(value: 'Alpha')
+
+ change_currency_to(eur)
+
+ expect(page).not_to have_content('Manufacturer')
+
+ click_on 'Brand'
+ expect(page).to have_filter_with(value: 'Gamma')
+ expect(page).not_to have_filter_with(value: 'Zeta')
+ expect(page).not_to have_filter_with(value: 'Alpha')
+ end
+
+ it 'correctly displays filterable options' do
+ visit_taxons_page(taxon)
+
+ expect(page).to have_content('Color')
+
+ click_on 'Size'
+ expect(page).to have_filter_with(value: 'S')
+ expect(page).to have_filter_with(value: 'M')
+
+ change_currency_to(eur)
+
+ expect(page).not_to have_content('Color')
+
+ click_on 'Size'
+ expect(page).to have_filter_with(value: 'XL')
+ expect(page).not_to have_filter_with(value: 'S')
+ expect(page).not_to have_filter_with(value: 'M')
+ end
+ end
+
+ context 'when switching between taxons' do
+ let!(:other_taxon) { create(:taxon) }
+
+ let!(:xl_size) { create(:option_value, option_type: size, name: 'xl', presentation: 'XL') }
+ let!(:gamma_brand) { create(:product_property, value: 'Gamma', property: brand) }
+
+ let!(:product_3) { create(:product, name: '3rd shirt', product_properties: [gamma_brand], taxons: [other_taxon]) }
+ let!(:variant_3) { create(:variant, product: product_3, option_values: [xl_size]) }
+
+ let!(:wannabe_manufacturer) { create(:product_property, value: 'Wannabe', property: manufacturer) }
+ let!(:product_4) { create(:product, name: '4th shirt', product_properties: [wannabe_manufacturer], taxons: [other_taxon]) }
+
+ it 'correctly displays filterable properties' do
+ visit_taxons_page(taxon)
+
+ click_on 'Manufacturer'
+ expect(page).to have_filter_with(value: 'Wilson')
+
+ click_on 'Brand'
+ expect(page).to have_filter_with(value: 'Zeta')
+ expect(page).to have_filter_with(value: 'Alpha')
+
+ visit_taxons_page(other_taxon)
+
+ click_on 'Manufacturer'
+ expect(page).to have_filter_with(value: 'Wannabe')
+ expect(page).not_to have_filter_with(value: 'Wilson')
+
+ click_on 'Brand'
+ expect(page).to have_filter_with(value: 'Gamma')
+ expect(page).not_to have_filter_with(value: 'Alpha')
+ expect(page).not_to have_filter_with(value: 'Zeta')
+ end
+
+ it 'correctly displays filterable options' do
+ visit_taxons_page(taxon)
+
+ expect(page).to have_content('Color')
+
+ click_on 'Size'
+ expect(page).to have_filter_with(value: 'S')
+ expect(page).to have_filter_with(value: 'M')
+
+ visit_taxons_page(other_taxon)
+
+ expect(page).not_to have_content('Color')
+
+ click_on 'Size'
+ expect(page).to have_filter_with(value: 'XL')
+ expect(page).not_to have_filter_with(value: 'S')
+ expect(page).not_to have_filter_with(value: 'M')
+ end
+ end
+
+ context 'when after visiting products page new products were added to the same taxon' do
+ let!(:material) { create(:property, :material, filterable: true) }
+ let!(:cotton_material) { create(:product_property, value: 'Cotton', property: material) }
+
+ let!(:xl_size) { create(:option_value, option_type: size, name: 'xl', presentation: 'XL') }
+
+ it 'correctly displays filterable properties' do
+ visit_taxons_page(taxon)
+
+ click_on 'Manufacturer'
+ expect(page).to have_filter_with(value: 'Wilson')
+
+ click_on 'Brand'
+ expect(page).to have_filter_with(value: 'Zeta')
+ expect(page).to have_filter_with(value: 'Alpha')
+
+ create(:product, name: '3rd shirt', product_properties: [cotton_material], taxons: [taxon])
+
+ visit_taxons_page(taxon)
+
+ click_on 'Material'
+ expect(page).to have_filter_with(value: 'Cotton')
+ end
+
+ it 'correctly displays filterable options' do
+ visit_taxons_page(taxon)
+
+ expect(page).to have_content('Color')
+
+ click_on 'Size'
+ expect(page).to have_filter_with(value: 'S')
+ expect(page).to have_filter_with(value: 'M')
+
+ product = create(:product, name: '3rd shirt', taxons: [taxon])
+ create(:variant, product: product, option_values: [xl_size])
+
+ visit_taxons_page(taxon)
+
+ click_on 'Size'
+ expect(page).to have_filter_with(value: 'XL')
+ end
+ end
+ end
end