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.

Added Product#taxons_for_store method

Damian Legawiecaa6044a89861
diff --git a/api/app/serializers/spree/api/v2/platform/product_serializer.rb b/api/app/serializers/spree/api/v2/platform/product_serializer.rb
index d874d3d086..748adcebc3 100644
--- a/api/app/serializers/spree/api/v2/platform/product_serializer.rb
+++ b/api/app/serializers/spree/api/v2/platform/product_serializer.rb
@@ -59,7 +59,9 @@ module Spree
has_many :variants
has_many :option_types
has_many :product_properties
- has_many :taxons
+ has_many :taxons do |object, params|
+ object.taxons_for_store(params[:store])
+ end
has_many :images,
object_method_name: :variant_images,
diff --git a/api/app/serializers/spree/v2/storefront/product_serializer.rb b/api/app/serializers/spree/v2/storefront/product_serializer.rb
index 10c8364750..25cfbfd7a7 100644
--- a/api/app/serializers/spree/v2/storefront/product_serializer.rb
+++ b/api/app/serializers/spree/v2/storefront/product_serializer.rb
@@ -49,7 +49,7 @@ module Spree
has_many :product_properties
has_many :taxons do |object, params|
- object.taxons.for_store(params[:store])
+ object.taxons_for_store(params[:store])
end
# all images from all variants
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 115d64f458..fd4df7c08f 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -323,6 +323,12 @@ module Spree
@category ||= taxons.joins(:taxonomy).order(depth: :desc).find_by(spree_taxonomies: { name: Spree.t(:taxonomy_categories_name) })
end
+ def taxons_for_store(store)
+ Rails.cache.fetch("#{cache_key_with_version}/taxons-per-store/#{store.id}") do
+ taxons.for_store(store)
+ end
+ end
+
private
def add_associations_from_prototype
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index cc958752e0..a00b792fea 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -783,4 +783,22 @@ describe Spree::Product, type: :model do
end
end
end
+
+ describe '#taxons_for_store' do
+ let(:store) { create(:store) }
+ let(:store_2) { create(:store) }
+ let(:product) { create(:product, stores: [store, store_2], taxons: [taxon, taxon_2]) }
+ let(:taxonomy) { create(:taxonomy, store: store) }
+ let(:taxonomy_2) { create(:taxonomy, store: store_2) }
+ let(:taxon) { create(:taxon, taxonomy: taxonomy) }
+ let(:taxon_2) { create(:taxon, taxonomy: taxonomy_2) }
+ let(:taxon_3) { create(:taxon, taxonomy: taxonomy) }
+
+ it 'returns product taxons for specified store' do
+ expect(product.taxons_for_store(store)).to eq([taxon])
+ expect(product.taxons_for_store(store_2)).to eq([taxon_2])
+ end
+
+ it { expect(product.taxons_for_store(store)).to be_a(ActiveRecord::Relation) }
+ end
end