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.

[CSV Import] fix assigning product to store (#14051)

Kacper Mękarski4043d9e2f083
diff --git a/spree/core/app/models/spree/import.rb b/spree/core/app/models/spree/import.rb
index 9956739a6c..be46f95e3e 100644
--- a/spree/core/app/models/spree/import.rb
+++ b/spree/core/app/models/spree/import.rb
@@ -62,6 +62,7 @@ module Spree
event :complete do
transition from: :processing, to: :completed
end
+ after_transition to: :completed, do: :touch_store
after_transition to: :completed, do: :publish_import_completed_event
# NOTE: send_import_completed_email and update_loader_in_import_view
# are now handled by Spree::Admin::ImportSubscriber listening to 'import.completed' event
@@ -181,6 +182,10 @@ module Spree
"#{Spree.t(type.demodulize.pluralize.downcase)} #{number}"
end
+ def touch_store
+ store.touch
+ end
+
def publish_import_completed_event
publish_event('import.completed')
end
diff --git a/spree/core/app/services/spree/imports/row_processors/product_variant.rb b/spree/core/app/services/spree/imports/row_processors/product_variant.rb
index d8466fda82..4c7de55a41 100644
--- a/spree/core/app/services/spree/imports/row_processors/product_variant.rb
+++ b/spree/core/app/services/spree/imports/row_processors/product_variant.rb
@@ -66,8 +66,12 @@ module Spree
product = existing_product if existing_product.present?
end
- product = assign_attributes_to_product(product)
- product.save!
+ # Store is touched when the import completes
+ Spree::Store.no_touching do
+ product = assign_attributes_to_product(product)
+ product.save!
+ end
+
if has_product_attributes?
handle_metafields(product)
handle_categories(product)
diff --git a/spree/core/spec/models/spree/import_spec.rb b/spree/core/spec/models/spree/import_spec.rb
index db3fd18d35..523f3ec84d 100644
--- a/spree/core/spec/models/spree/import_spec.rb
+++ b/spree/core/spec/models/spree/import_spec.rb
@@ -95,6 +95,10 @@ RSpec.describe Spree::Import, :job, type: :model do
expect { import.complete! }.to change(import, :status).from('processing').to('completed')
end
+ it 'touches the store' do
+ expect { import.complete! }.to change { store.reload.updated_at }
+ end
+
it 'publishes import.completed event' do
expect(import).to receive(:publish_import_completed_event)
import.complete!
diff --git a/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb b/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb
index 55d6cb0c40..c03fa31163 100644
--- a/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb
+++ b/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb
@@ -56,6 +56,10 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service do
expect(variant.stock_items.first.stock_location).to eq store.default_stock_location
end
+ it 'does not touch the store when associating the product' do
+ expect { subject.process! }.not_to change { store.reload.updated_at }
+ end
+
context 'when updating an existing master variant' do
let!(:existing_product) { create(:product, slug: 'denim-shirt', name: 'Old Name', stores: [store]) }