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 `Spree::Variant.product_name_or_sku_cont` scope when using translations (#12463)

Łukasz Adamczyk19f00fa9c245
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index f1a0162370..2fe29a6b84 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -187,7 +187,9 @@ module Spree
sku_condition = arel_table[:sku].lower.matches(query_pattern)
if Spree.use_translations?
- product_name_condition = Product.translation_table[:name].lower.matches(query_pattern)
+ translation_arel_table = Product::Translation.arel_table.alias(Product.translation_table_alias)[:name]
+ product_name_condition = translation_arel_table.lower.matches(query_pattern)
+
joins(:product).
join_translation_table(Product).
where(product_name_condition.or(sku_condition))
diff --git a/core/spec/models/spree/variant/scopes_spec.rb b/core/spec/models/spree/variant/scopes_spec.rb
index c22bb7ce6a..8a46b98db3 100644
--- a/core/spec/models/spree/variant/scopes_spec.rb
+++ b/core/spec/models/spree/variant/scopes_spec.rb
@@ -45,6 +45,28 @@ describe 'Variant scopes', type: :model do
expect(variants).not_to include(variant_1, variant_2, variant_3)
expect(variants.count).to eq(0)
end
+
+ context 'when using translations' do
+ before do
+ I18n.with_locale(:pl) do
+ product_1.update!(name: 'Produkt pierwszy')
+ product_2.update!(name: 'Produkt drugi')
+ product_3.update!(name: 'Produkt trzeci')
+ end
+ end
+
+ it 'returns variants based on products translated name' do
+ I18n.with_locale(:pl) do
+ expect(Spree::Variant.product_name_or_sku_cont('Drugi')).to include(variant_2)
+ end
+ end
+
+ it 'does not return variants of products that do not match name' do
+ I18n.with_locale(:pl) do
+ expect(Spree::Variant.product_name_or_sku_cont('Pierwszy')).not_to include(variant_2, variant_3)
+ end
+ end
+ end
end
describe '#search_by_product_name_or_sku' do