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.

show Filters for Products from the Current Store only

Łukasz Adamczyk71e9a3b3449e
diff --git a/core/app/models/concerns/spree/product_scopes.rb b/core/app/models/concerns/spree/product_scopes.rb
index 7298b0a698..048d2b9535 100644
--- a/core/app/models/concerns/spree/product_scopes.rb
+++ b/core/app/models/concerns/spree/product_scopes.rb
@@ -250,9 +250,10 @@ module Spree
end
search_scopes << :active
- def self.for_filters(currency, taxon = nil)
+ def self.for_filters(currency, taxon: nil, store: nil)
scope = active(currency)
scope = scope.in_taxon(taxon) if taxon.present?
+ scope = scope.by_store(store) if store.present?
scope
end
search_scopes << :for_filters
diff --git a/core/spec/models/spree/product/scopes_spec.rb b/core/spec/models/spree/product/scopes_spec.rb
index 6c60f6aca1..4e4ab96dfe 100644
--- a/core/spec/models/spree/product/scopes_spec.rb
+++ b/core/spec/models/spree/product/scopes_spec.rb
@@ -43,18 +43,29 @@ describe 'Product scopes', type: :model do
let(:taxon_1) { create(:taxon) }
let(:taxon_2) { create(:taxon) }
- let!(:product_1) { create(:product, currency: 'GBP', taxons: [taxon_1]) }
- let!(:product_2) { create(:product, currency: 'GBP', taxons: [taxon_2]) }
+ let(:store_1) { create(:store) }
+ let(:store_2) { create(:store) }
+
+ let!(:product_1) { create(:product, currency: 'GBP', taxons: [taxon_1], stores: [store_1]) }
+ let!(:product_2) { create(:product, currency: 'GBP', taxons: [taxon_2], stores: [store_2]) }
before do
create(:product, currency: 'USD', taxons: [create(:taxon)])
end
context 'when giving a taxon' do
- it { expect(subject.call('GBP', taxon_1)).to contain_exactly(product_1) }
+ it { expect(subject.call('GBP', taxon: taxon_1)).to contain_exactly(product_1) }
+ end
+
+ context 'when giving a store' do
+ it { expect(subject.call('GBP', store: store_2)).to contain_exactly(product_2) }
+ end
+
+ context 'when giving both taxon and store' do
+ it { expect(subject.call('GBP', taxon: taxon_1, store: store_1)).to contain_exactly(product_1) }
end
- context 'when giving no taxon' do
+ context 'when giving no taxon and store' do
it { expect(subject.call('GBP')).to contain_exactly(product_1, product_2) }
end
diff --git a/frontend/app/helpers/spree/frontend_helper.rb b/frontend/app/helpers/spree/frontend_helper.rb
index 1830fa63da..596c9ed3d5 100644
--- a/frontend/app/helpers/spree/frontend_helper.rb
+++ b/frontend/app/helpers/spree/frontend_helper.rb
@@ -384,7 +384,7 @@ module Spree
end
def products_for_filters
- Product.for_filters(current_currency, @taxon)
+ Product.for_filters(current_currency, taxon: @taxon, store: current_store)
end
def products_for_filters_cache_key
diff --git a/frontend/spec/features/products_filtering_spec.rb b/frontend/spec/features/products_filtering_spec.rb
index 54e3f29e39..67897ae4ae 100644
--- a/frontend/spec/features/products_filtering_spec.rb
+++ b/frontend/spec/features/products_filtering_spec.rb
@@ -128,6 +128,18 @@ describe 'Products filtering', :js do
end
context 'option type filters' do
+ let!(:length) { create :option_type, name: 'length', presentation: 'Length', filterable: true }
+ let!(:mini_length) { create :option_value, option_type: length, name: 'mini', presentation: 'MINI' }
+
+ let!(:product_3) { create :product, stores: [create(:store)], taxons: [taxon], option_types: [length] }
+ let!(:variant_3) { create :variant, product: product_3, option_values: [mini_length] }
+
+ it 'does not display option types for products assigned to the other store' do
+ visit spree.nested_taxons_path(taxon)
+
+ expect(filters).not_to have_content('Length')
+ end
+
it 'displays filterable option types' do
visit spree.nested_taxons_path(taxon)
@@ -145,6 +157,17 @@ describe 'Products filtering', :js do
end
context 'property filters' do
+ let!(:material) { create :property, :material, filterable: true }
+ let!(:cotton_material) { create :product_property, value: 'Cotton', property: material }
+
+ let!(:product_3) { create :product, stores: [create(:store)], taxons: [taxon], product_properties: [cotton_material] }
+
+ it 'does not display properties for products assigned to the other store' do
+ visit spree.nested_taxons_path(taxon)
+
+ expect(filters).not_to have_content('Material')
+ end
+
it 'displays filterable properties' do
visit spree.nested_taxons_path(taxon)
diff --git a/frontend/spec/features/products_spec.rb b/frontend/spec/features/products_spec.rb
index dd0fffdd62..1855dae511 100644
--- a/frontend/spec/features/products_spec.rb
+++ b/frontend/spec/features/products_spec.rb
@@ -600,4 +600,32 @@ describe 'Visiting Products', type: :feature, inaccessible: true do
expect(page).not_to have_content('9377-AMZ-1837')
end
end
+
+ context 'for multiple stores' do
+ let(:other_store) { create(:store) }
+
+ before do
+ create(:product, stores: [other_store], name: 'Other Store Product 1')
+ create(:product, stores: [other_store], name: 'Other Store Product 2')
+ end
+
+ it 'shows products from the current store only' do
+ visit spree.products_path
+
+ expect(page).to have_css('.product-component-name').exactly(9).times
+
+ 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 Mug',
+ 'Ruby on Rails Tote',
+ '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
+ end
end