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 importing products with store whose default locale differs from app default (#14349)

Kacper Mękarski2293ef8848f9
diff --git a/spree/core/app/jobs/spree/imports/base_job.rb b/spree/core/app/jobs/spree/imports/base_job.rb
index 3ef71d23d7..ff35eabb29 100644
--- a/spree/core/app/jobs/spree/imports/base_job.rb
+++ b/spree/core/app/jobs/spree/imports/base_job.rb
@@ -10,6 +10,19 @@ module Spree
# taxon creation races).
class BaseJob < Spree::BaseJob
queue_as Spree.queues.imports
+
+ private
+
+ def with_store_content_locale(store, &block)
+ locale = store&.default_locale
+ return yield if locale.blank?
+
+ previous_content_locale = Spree::Current.content_locale
+ Spree::Current.content_locale = locale
+ I18n.with_locale(locale, &block)
+ ensure
+ Spree::Current.content_locale = previous_content_locale unless locale.blank?
+ end
end
end
end
diff --git a/spree/core/app/jobs/spree/imports/create_categories_job.rb b/spree/core/app/jobs/spree/imports/create_categories_job.rb
index 94973c37bb..f6dd34477a 100644
--- a/spree/core/app/jobs/spree/imports/create_categories_job.rb
+++ b/spree/core/app/jobs/spree/imports/create_categories_job.rb
@@ -9,9 +9,11 @@ module Spree
def perform(product_id, store_id, taxon_pretty_names)
product = Spree::Product.find(product_id)
store = Spree::Store.find(store_id)
- taxons = taxon_pretty_names.filter_map { |taxon_pretty_name| find_or_create_taxon(store, taxon_pretty_name) }
- product.taxons = taxons
+ with_store_content_locale(store) do
+ taxons = taxon_pretty_names.filter_map { |taxon_pretty_name| find_or_create_taxon(store, taxon_pretty_name) }
+ product.taxons = taxons
+ end
end
private
diff --git a/spree/core/app/jobs/spree/imports/process_group_job.rb b/spree/core/app/jobs/spree/imports/process_group_job.rb
index df1c1825af..d57413c9da 100644
--- a/spree/core/app/jobs/spree/imports/process_group_job.rb
+++ b/spree/core/app/jobs/spree/imports/process_group_job.rb
@@ -25,43 +25,45 @@ module Spree
import = Spree::Import.find(import_id)
Spree::Current.store = import.store
- mappings = import.mappings.mapped.to_a
- schema_fields = import.schema_fields
- large = import.large_import?
- grouped = import.group_column.present? && mappings.any? { |m| m.schema_field == import.group_column }
- started_at = Time.current
- processed_rows = []
+ with_store_content_locale(import.store) do
+ mappings = import.mappings.mapped.to_a
+ schema_fields = import.schema_fields
+ large = import.large_import?
+ grouped = import.group_column.present? && mappings.any? { |m| m.schema_field == import.group_column }
+ started_at = Time.current
+ processed_rows = []
- row_ids.each_slice(ROWS_BATCH_SIZE) do |ids|
- # Skip rows already completed on a prior attempt so retries don't double-process them.
- rows = import.rows.where(id: ids).pending_and_failed.order(:row_number).to_a
- # Share the already-loaded import across rows: each row's processor reads
- # `row.import` (store, ability, lookup cache), and without this every row
- # lazily loads its own Import instance and rebuilds all of that per row.
- rows.each { |row| row.association(:import).target = import }
+ row_ids.each_slice(ROWS_BATCH_SIZE) do |ids|
+ # Skip rows already completed on a prior attempt so retries don't double-process them.
+ rows = import.rows.where(id: ids).pending_and_failed.order(:row_number).to_a
+ # Share the already-loaded import across rows: each row's processor reads
+ # `row.import` (store, ability, lookup cache), and without this every row
+ # lazily loads its own Import instance and rebuilds all of that per row.
+ rows.each { |row| row.association(:import).target = import }
- if large
- Spree::Events.disable do
- rows.each { |row| row.bulk_process!(mappings: mappings, schema_fields: schema_fields) }
- end
- elsif grouped
- # A group is one product plus its variants: per-record lifecycle
- # events (variant.created, price.created, product.updated per
- # touch) are noise to subscribers — one product event is published
- # for the whole group below. import_row.* events still flow.
- Spree::Events.disable_lifecycle do
- rows.each { |row| row.process!(mappings: mappings, schema_fields: schema_fields) }
- end
- else
- rows.each do |row|
- row.process!(mappings: mappings, schema_fields: schema_fields)
+ if large
+ Spree::Events.disable do
+ rows.each { |row| row.bulk_process!(mappings: mappings, schema_fields: schema_fields) }
+ end
+ elsif grouped
+ # A group is one product plus its variants: per-record lifecycle
+ # events (variant.created, price.created, product.updated per
+ # touch) are noise to subscribers — one product event is published
+ # for the whole group below. import_row.* events still flow.
+ Spree::Events.disable_lifecycle do
+ rows.each { |row| row.process!(mappings: mappings, schema_fields: schema_fields) }
+ end
+ else
+ rows.each do |row|
+ row.process!(mappings: mappings, schema_fields: schema_fields)
+ end
end
+ processed_rows.concat(rows)
end
- processed_rows.concat(rows)
- end
- publish_group_events(processed_rows, started_at) if grouped
- check_import_completion(import, large)
+ publish_group_events(processed_rows, started_at) if grouped
+ check_import_completion(import, large)
+ end
end
private
diff --git a/spree/core/spec/jobs/spree/imports/create_categories_job_spec.rb b/spree/core/spec/jobs/spree/imports/create_categories_job_spec.rb
index ced0ab3bd7..3e9ad12790 100644
--- a/spree/core/spec/jobs/spree/imports/create_categories_job_spec.rb
+++ b/spree/core/spec/jobs/spree/imports/create_categories_job_spec.rb
@@ -37,6 +37,33 @@ RSpec.describe Spree::Imports::CreateCategoriesJob, type: :job do
expect(product.reload.taxons.map(&:pretty_name)).to contain_exactly('Men -> Clothing')
end
+ context "when the store's default locale differs from I18n.default_locale" do
+ before { store.update_columns(default_locale: 'de') }
+
+ it 'populates the NOT NULL base name columns instead of leaving them null' do
+ # Mimics the enqueue-time locale a non-en store's request captures into the job.
+ I18n.with_locale(:de) do
+ described_class.perform_now(product.id, store.id, ['Men -> Clothing'])
+ end
+
+ taxon = product.reload.taxons.first
+ expect(taxon).to be_present
+ # The raw NOT NULL base columns (not the translation) must be populated.
+ expect(taxon.read_attribute(:name)).to eq('Clothing')
+ expect(taxon.taxonomy.read_attribute(:name)).to eq('Men')
+ end
+
+ it "keeps Spree::Taxon's Mobility.with_locale(nil) permalink regeneration working" do
+ I18n.with_locale(:de) do
+ described_class.perform_now(product.id, store.id, ['Men -> Clothing'])
+ end
+
+ taxon = product.reload.taxons.first
+ expect(taxon.read_attribute(:permalink)).to eq('men/clothing')
+ expect(taxon.read_attribute(:pretty_name)).to eq('Men -> Clothing')
+ end
+ end
+
context 'when taxonomies and taxons already exist' do
let!(:men_taxonomy) { create(:taxonomy, name: 'Men', store: store) }
let!(:clothing_taxon) { create(:taxon, name: 'Clothing', taxonomy: men_taxonomy, parent: men_taxonomy.root) }
diff --git a/spree/core/spec/jobs/spree/imports/process_group_job_spec.rb b/spree/core/spec/jobs/spree/imports/process_group_job_spec.rb
index c03cbdd887..2e7b9a077d 100644
--- a/spree/core/spec/jobs/spree/imports/process_group_job_spec.rb
+++ b/spree/core/spec/jobs/spree/imports/process_group_job_spec.rb
@@ -300,6 +300,24 @@ RSpec.describe Spree::Imports::ProcessGroupJob, type: :job do
end
end
+ context "when the store's default locale differs from I18n.default_locale" do
+ before { store.update_columns(default_locale: 'de') }
+
+ it 'authors the base columns in the store locale instead of leaving them null' do
+ # Mimics the enqueue-time locale a non-en store's request captures into the job.
+ I18n.with_locale(:de) do
+ expect {
+ described_class.perform_now(import.id, [row.id])
+ }.to change(Spree::Product, :count).by(1)
+ end
+
+ row.reload
+ expect(row.status).to eq('completed')
+ # The raw NOT NULL base column (not the translation) must be populated.
+ expect(row.item.product.read_attribute(:name)).to eq('Test Product')
+ end
+ end
+
describe 'atomic completion tracking' do
it 'uses atomic SQL increment for completed_groups_count' do
# Leave another row pending so the import doesn't finalize on this run; we want