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
add the Spree::Product.for_filters scope
Łukasz Adamczyk8574d47da8c6
diff --git a/core/app/models/concerns/spree/product_scopes.rb b/core/app/models/concerns/spree/product_scopes.rbindex 76009d8951..166600e43a 100644--- a/core/app/models/concerns/spree/product_scopes.rb+++ b/core/app/models/concerns/spree/product_scopes.rb@@ -233,6 +233,13 @@ module Spreeendsearch_scopes << :active+ def self.for_filters(currency, taxon = nil)+ scope = active(currency)+ scope = scope.in_taxon(taxon) if taxon.present?+ scope+ end+ search_scopes << :for_filters+def self.for_user(user = nil)if user.try(:has_spree_role?, 'admin')with_deleteddiff --git a/core/spec/models/spree/product/scopes_spec.rb b/core/spec/models/spree/product/scopes_spec.rbindex 98e4e801f2..cf35330f6e 100644--- a/core/spec/models/spree/product/scopes_spec.rb+++ b/core/spec/models/spree/product/scopes_spec.rb@@ -37,6 +37,32 @@ describe 'Product scopes', type: :model doendend+ describe '.for_filters' do+ subject { Spree::Product.method(:for_filters) }++ 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]) }++ 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) }+ end++ context 'when giving no taxon' do+ it { expect(subject.call('GBP')).to contain_exactly(product_1, product_2) }+ end++ context 'when giving a currency with no products' do+ it { expect(subject.call('PLN')).to be_empty }+ end+ end+context 'A product assigned to parent and child taxons' dobefore do@taxonomy = create(:taxonomy)diff --git a/frontend/app/helpers/spree/frontend_helper.rb b/frontend/app/helpers/spree/frontend_helper.rbindex 4842b8014e..efda927fa0 100644--- a/frontend/app/helpers/spree/frontend_helper.rb+++ b/frontend/app/helpers/spree/frontend_helper.rb@@ -299,7 +299,7 @@ module Spreedef available_option_types@available_option_types ||= Rails.cache.fetch("available-option-types/#{available_option_types_cache_key}") do- option_values = OptionValues::FindAvailable.new(products_scope: products_for_filtering).execute+ option_values = OptionValues::FindAvailable.new(products_scope: products_for_filters).executeFilters::Options.new(option_values_scope: option_values).to_aendend@@ -310,7 +310,7 @@ module Spreedef available_properties@available_properties ||= Rails.cache.fetch("available-properties/#{available_properties_cache_key}") do- product_properties = ProductProperties::FindAvailable.new(products_scope: products_for_filtering).execute+ product_properties = ProductProperties::FindAvailable.new(products_scope: products_for_filters).executeFilters::Properties.new(product_properties_scope: product_properties).to_aendend@@ -365,10 +365,8 @@ module Spreeendend- def products_for_filtering- scope = Product.active(current_currency)- scope = scope.in_taxon(@taxon) if @taxon.present?- scope+ def products_for_filters+ Product.for_filters(current_currency, @taxon)endendend