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.

Auto-assign default channel when importing products via CSV (#14179)

Damian Legawiecef587e1aefa8
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 b2fcf48e33..97c61ef245 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
@@ -99,6 +99,9 @@ module Spree
product.slug = attributes['slug']
product.sku = attributes['sku'] if attributes['sku'].present? && options.empty?
product.store = store
+ # Publish to the store's default channel so imported products surface
+ # in the storefront.
+ product.channels << store.default_channel if store.default_channel && product.channels.empty?
end
product.name = attributes['name'] if attributes['name'].present?
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 a6ff9f10e4..ddf7386136 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
@@ -63,6 +63,25 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service do
expect { subject.process! }.not_to change { store.reload.updated_at }
end
+ it 'publishes the product to the store default channel' do
+ product = variant.product
+
+ expect(product.channels).to contain_exactly(store.default_channel)
+ end
+
+ context 'when the store has no default channel' do
+ # Simulate a store that predates channels: delete_all skips the
+ # before_destroy guard that protects the default channel.
+ before { store.channels.delete_all }
+
+ it 'imports the product without publishing it to any channel' do
+ product = variant.product
+
+ expect(product).to be_persisted
+ expect(product.channels).to be_empty
+ end
+ end
+
context 'when updating an existing master variant' do
let!(:existing_product) { create(:product, slug: 'denim-shirt', name: 'Old Name') }
@@ -85,6 +104,10 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service do
expect(stock_item.backorderable).to eq true
expect(existing_product.reload.name).to eq 'Denim Shirt'
end
+
+ it 'leaves the existing channel publication untouched' do
+ expect { subject.process! }.not_to change { existing_product.reload.channels.to_a }
+ end
end
context 'when tags are not present' do