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.

Fix deleting products w/o translations on non-default locale (#13613)

Łukasz Adamczykcd5f06622de6
diff --git a/backend/engines/core/app/models/spree/product/slugs.rb b/backend/engines/core/app/models/spree/product/slugs.rb
index f48f357134..459d13a343 100644
--- a/backend/engines/core/app/models/spree/product/slugs.rb
+++ b/backend/engines/core/app/models/spree/product/slugs.rb
@@ -103,7 +103,7 @@ module Spree
translations.with_deleted.each { |rec| rec.update_columns(slug: new_slug.call(rec)) }
slugs.with_deleted.each { |rec| rec.update_column(:slug, new_slug.call(rec)) }
- translations.find_by!(locale: I18n.locale).update_column(:slug, slug) if Spree.use_translations?
+ translations.find_by(locale: I18n.locale)&.update_column(:slug, slug) if Spree.use_translations?
end
end
end
diff --git a/backend/engines/core/spec/models/spree/product/slugs_spec.rb b/backend/engines/core/spec/models/spree/product/slugs_spec.rb
index 9c5d0149f2..cc09b5ae98 100644
--- a/backend/engines/core/spec/models/spree/product/slugs_spec.rb
+++ b/backend/engines/core/spec/models/spree/product/slugs_spec.rb
@@ -105,6 +105,35 @@ describe Spree::Product::Slugs, type: :model do
end
end
+ context 'when using translations' do
+ let(:product) do
+ I18n.with_locale(:en) { create(:product, stores: [store], slug: product_slug) }
+ end
+
+ before { I18n.locale = :fr }
+ after { I18n.locale = :en }
+
+ context 'when product translation exists with a translated slug' do
+ before do
+ I18n.with_locale(:fr) { product.update!(name: 'French product') }
+ end
+
+ it 'renames slug for the translation' do
+ product.destroy!
+ expect(product.translations.with_deleted.where(locale: :fr).first.slug).to match(/deleted-french-product-.+/)
+ end
+ end
+
+ context 'when product translation does not exist' do
+ it 'does nothing' do
+ product.destroy!
+
+ expect(product.deleted_at).to be_present
+ expect(product.translations.with_deleted).to be_empty
+ end
+ end
+ end
+
context 'when slug is already at or near max length' do
before do
product.slug = nil