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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex 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 Spreeproduct.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?endproduct.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.rbindex 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 doexpect { 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' dolet!(:existing_product) { create(:product, slug: 'denim-shirt', name: 'Old Name') }@@ -85,6 +104,10 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service doexpect(stock_item.backorderable).to eq trueexpect(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 }+ endendcontext 'when tags are not present' do