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
Localized_slugs added to product endpoint
kpokorski8597232ae88f
diff --git a/api/app/serializers/spree/v2/storefront/product_serializer.rb b/api/app/serializers/spree/v2/storefront/product_serializer.rbindex 57f50ec7d4..106ae9f12b 100644--- a/api/app/serializers/spree/v2/storefront/product_serializer.rb+++ b/api/app/serializers/spree/v2/storefront/product_serializer.rb@@ -24,6 +24,10 @@ module Spreeproduct.available?end+ attribute :localized_slugs do |product|+ product.localized_slugs+ end+attribute :currency do |_product, params|params[:currency]enddiff --git a/api/app/serializers/spree/v2/storefront/taxon_serializer.rb b/api/app/serializers/spree/v2/storefront/taxon_serializer.rbindex 762a69510b..270abfa997 100644--- a/api/app/serializers/spree/v2/storefront/taxon_serializer.rb+++ b/api/app/serializers/spree/v2/storefront/taxon_serializer.rb@@ -19,6 +19,10 @@ module Spreetaxon.leaf?end+ attribute :localized_slugs do |taxon|+ taxon.localized_slugs+ end+belongs_to :parent, record_type: :taxon, serializer: :taxonbelongs_to :taxonomy, record_type: :taxonomydiff --git a/api/spec/requests/spree/api/v2/storefront/products_spec.rb b/api/spec/requests/spree/api/v2/storefront/products_spec.rbindex b105d7474f..c9b42e17f8 100644--- a/api/spec/requests/spree/api/v2/storefront/products_spec.rb+++ b/api/spec/requests/spree/api/v2/storefront/products_spec.rb@@ -994,6 +994,21 @@ describe 'API V2 Storefront Products Spec', type: :request doit_behaves_like 'returns 404 HTTP status'end+ context 'with localized_slugs' do+ let!(:product_with_slug) { create(:product) }+ let!(:translation1) { product_with_slug.translations.create(slug: 'test_slug_pl', locale: 'pl') }+ let!(:translation2) { product_with_slug.translations.create(slug: 'test_slug_es', locale: 'es') }++ before do+ get "/api/v2/storefront/products/#{product_with_slug.id}"+ end++ it 'returns translated slugs' do+ expect(json_response['data']['attributes']['localized_slugs']).to match(product_with_slug.localized_slugs)+ expect(json_response['data']['attributes']['localized_slugs']).to be_an_instance_of(ActiveSupport::HashWithIndifferentAccess)+ end+ end+context 'with product image data' doshared_examples 'returns product image data' doit 'returns product image data' dodiff --git a/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb b/api/spec/requests/spree/api/v2/storefront/taxons_spec.rbindex eabd678c84..b7eec986dc 100644--- a/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb+++ b/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb@@ -218,6 +218,20 @@ describe 'Taxons Spec', type: :request doendend+ context 'with localized_slugs' do+ let!(:taxon_with_slug) { create(:taxon) }+ let!(:translations) { taxon_with_slug.translations.create([{ slug: 'test_slug_pl', locale: 'pl' }, { slug: 'test_slug_es', locale: 'es' } ])}++ before do+ get "/api/v2/storefront/taxons/#{taxon_with_slug.id}"+ end++ it 'returns translated slugs' do+ expect(json_response['data']['attributes']['localized_slugs']).to match(taxon_with_slug.localized_slugs)+ expect(json_response['data']['attributes']['localized_slugs']).to be_an_instance_of(ActiveSupport::HashWithIndifferentAccess)+ end+ end+context 'with taxon image data' doshared_examples 'returns taxon image data' doit 'returns taxon image data' dodiff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rbindex 2d74abecb6..59321d2f8e 100644--- a/core/app/models/spree/product.rb+++ b/core/app/models/spree/product.rb@@ -388,6 +388,10 @@ module Spreeshipping_category&.name == I18n.t('spree.seed.shipping.categories.digital')end+ def localized_slugs+ Hash[translations.pluck(:locale, :slug)]+ end+privatedef add_associations_from_prototypediff --git a/core/app/models/spree/taxon.rb b/core/app/models/spree/taxon.rbindex 6589fbf9a1..30b0088c95 100644--- a/core/app/models/spree/taxon.rb+++ b/core/app/models/spree/taxon.rb@@ -55,9 +55,13 @@ module Spreescope :for_stores, ->(stores) { joins(:taxonomy).where(spree_taxonomies: { store_id: stores.ids }) }- TRANSLATABLE_FIELDS = %i[name description].freeze+ TRANSLATABLE_FIELDS = %i[name description permalink].freezetranslates(*TRANSLATABLE_FIELDS)+ self::Translation.class_eval do+ alias_attribute :slug, :permalink+ end+# indicate which filters should be used for a taxon# this method should be customized to your own sitedef applicable_filters@@ -111,6 +115,10 @@ module Spreemove_to_child_with_index(parent, idx.to_i) unless new_record?end+ def localized_slugs+ Hash[translations.pluck(:locale, :slug)]+ end+privatedef sync_taxonomy_name