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.

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.rb
index 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 Spree
product.available?
end
+ attribute :localized_slugs do |product|
+ product.localized_slugs
+ end
+
attribute :currency do |_product, params|
params[:currency]
end
diff --git a/api/app/serializers/spree/v2/storefront/taxon_serializer.rb b/api/app/serializers/spree/v2/storefront/taxon_serializer.rb
index 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 Spree
taxon.leaf?
end
+ attribute :localized_slugs do |taxon|
+ taxon.localized_slugs
+ end
+
belongs_to :parent, record_type: :taxon, serializer: :taxon
belongs_to :taxonomy, record_type: :taxonomy
diff --git a/api/spec/requests/spree/api/v2/storefront/products_spec.rb b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
index 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 do
it_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' do
shared_examples 'returns product image data' do
it 'returns product image data' do
diff --git a/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb b/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb
index 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 do
end
end
+ 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' do
shared_examples 'returns taxon image data' do
it 'returns taxon image data' do
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 2d74abecb6..59321d2f8e 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -388,6 +388,10 @@ module Spree
shipping_category&.name == I18n.t('spree.seed.shipping.categories.digital')
end
+ def localized_slugs
+ Hash[translations.pluck(:locale, :slug)]
+ end
+
private
def add_associations_from_prototype
diff --git a/core/app/models/spree/taxon.rb b/core/app/models/spree/taxon.rb
index 6589fbf9a1..30b0088c95 100644
--- a/core/app/models/spree/taxon.rb
+++ b/core/app/models/spree/taxon.rb
@@ -55,9 +55,13 @@ module Spree
scope :for_stores, ->(stores) { joins(:taxonomy).where(spree_taxonomies: { store_id: stores.ids }) }
- TRANSLATABLE_FIELDS = %i[name description].freeze
+ TRANSLATABLE_FIELDS = %i[name description permalink].freeze
translates(*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 site
def applicable_filters
@@ -111,6 +115,10 @@ module Spree
move_to_child_with_index(parent, idx.to_i) unless new_record?
end
+ def localized_slugs
+ Hash[translations.pluck(:locale, :slug)]
+ end
+
private
def sync_taxonomy_name