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
Fixes on CSV export/import: return option types and values for all variants, match option value correctly, distinguish no categories defined / unassigned categories (#14812)
Kacper Mękarskia1bc69275cb9
diff --git a/docs/user/manage-products/product-import-file-template.mdx b/docs/user/manage-products/product-import-file-template.mdxindex e580b42118..1562c48623 100644--- a/docs/user/manage-products/product-import-file-template.mdx+++ b/docs/user/manage-products/product-import-file-template.mdx@@ -9,6 +9,8 @@ For an example of **how to structure multi-variant products**, please download t<Note>You don’t need to follow the field naming conventions in this template exactly. Field names can be mapped manually during the import process.</Note>+<Note>A product with several variants takes one row per variant. The product's own details (name, description, status, categories) can either sit on a row of their own above the variants, or on the first variant's row — the layout Shopify exports use. Rows for the same product are matched by their shared **slug**.</Note>+<Warning>The **slug** field acts as the unique identifier for the product in Spree - no two products can share the same slug. When updating products via CSV import, the slug must exactly match an existing record to avoid creating duplicates.</Warning>| Field | Description |diff --git a/spree/core/app/models/concerns/spree/parameterizable_name.rb b/spree/core/app/models/concerns/spree/parameterizable_name.rbindex 48ff76c4c3..397c087d9e 100644--- a/spree/core/app/models/concerns/spree/parameterizable_name.rb+++ b/spree/core/app/models/concerns/spree/parameterizable_name.rb@@ -20,6 +20,8 @@ module Spreeendend+ scope :with_name, ->(value) { where(name: value.to_s.parameterize) }+def set_name_from_labelself.name = labelenddiff --git a/spree/core/app/presenters/spree/csv/product_variant_presenter.rb b/spree/core/app/presenters/spree/csv/product_variant_presenter.rbindex 092340abae..f0f543689e 100644--- a/spree/core/app/presenters/spree/csv/product_variant_presenter.rb+++ b/spree/core/app/presenters/spree/csv/product_variant_presenter.rb@@ -111,12 +111,12 @@ module Spreespree_image_url(variant.images[0], image_url_options),spree_image_url(variant.images[1], image_url_options),spree_image_url(variant.images[2], image_url_options),- index.positive? ? option_type(0)&.label : nil,- index.positive? ? option_value(option_type(0)) : nil,- index.positive? ? option_type(1)&.label : nil,- index.positive? ? option_value(option_type(1)) : nil,- index.positive? ? option_type(2)&.label : nil,- index.positive? ? option_value(option_type(2)) : nil+ option_type(0)&.label,+ option_value(option_type(0)),+ option_type(1)&.label,+ option_value(option_type(1)),+ option_type(2)&.label,+ option_value(option_type(2))]if index.zero?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 df5e08c085..a7a75571d3 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@@ -3,6 +3,7 @@ module Spreemodule RowProcessorsclass ProductVariant < BaseOPTION_TYPES_COUNT = 3+ CATEGORY_FIELDS = %w[category1 category2 category3].freezedef initialize(row, **)super@@ -91,9 +92,17 @@ module Spreeproduct.update_column(:default_variant_id, product.variants.reload.first&.id)end+ # Whether this row carries the product itself, as opposed to only one of+ # its variants. A row is a product row when it has no options at all, or+ # when it names the product alongside them — an export (and Shopify's+ # own CSV) writes the product's attributes on its first variant's row,+ # so that row has to both create the product and create a variant.+ def product_row?+ options.empty? || has_product_attributes?+ end+def ensure_product_exists- if options.empty?- # For product/header rows (no options), create or update the product+ if product_row?product = Spree::Product.newif attributes['slug'].present?existing_product = product_scope.find_by(slug: attributes['slug'].strip.downcase)@@ -167,9 +176,12 @@ module Spreeenddef assign_attributes_to_product(product)- # set the SKU on a product/header row so process! updates the default variant instead of creating a new oneif product.new_record?product.slug = attributes['slug']+ # Only an option-less row's SKU belongs to the product's own variant,+ # so process! updates that variant instead of creating a second one.+ # On a row that also carries options the SKU identifies that option+ # variant, which process! creates for itself.product.sku = attributes['sku'] if attributes['sku'].present? && options.empty?product.store = store# A seller's import creates the seller's products. Without this the@@ -197,11 +209,9 @@ module Spreeproduct.status = to_spree_status(attributes['status'])end- if options.empty?- if attributes['product_type'].present?- product_type = prepare_product_type- product.product_type = product_type if product_type.present?- end+ if attributes['product_type'].present?+ product_type = prepare_product_type+ product.product_type = product_type if product_type.present?endproduct@@ -304,11 +314,11 @@ module Spreedef find_or_create_option_type!(label)cached_lookup(:option_type, label) dobegin- store_option_types.search_by_name(label).first || store_option_types.create!(label: label)+ store_option_types.with_name(label).first || store_option_types.create!(label: label)rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid => eraise unless uniqueness_conflict?(e, :name)- store_option_types.search_by_name(label).first!+ store_option_types.with_name(label).first!endendend@@ -320,11 +330,11 @@ module Spreedef find_or_create_option_value!(option_type, label)cached_lookup(:option_value, option_type.id, label) dobegin- option_type.option_values.search_by_name(label).first || option_type.option_values.create!(label: label)+ option_type.option_values.with_name(label).first || option_type.option_values.create!(label: label)rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid => eraise unless uniqueness_conflict?(e, :name)- option_type.option_values.search_by_name(label).first!+ option_type.option_values.with_name(label).first!endendend@@ -375,7 +385,7 @@ module Spreeenddef has_product_attributes?- %w[name status description category1 category2 category3].any? { |key| attributes[key].present? }+ (%w[name status description] + CATEGORY_FIELDS).any? { |key| attributes[key].present? }enddef handle_custom_fields(product)@@ -442,6 +452,7 @@ module Spree# seller audience in the permission catalog at all.def handle_categories(product)return if seller.present?+ return unless category_columns_mapped?names = prepare_taxon_pretty_namesreturn Spree::Imports::CreateCategoriesJob.perform_now(product.id, store.id, names) if import.preferred_inline@@ -450,11 +461,19 @@ module Spreeenddef prepare_taxon_pretty_names- [- attributes['category1'],- attributes['category2'],- attributes['category3']- ].compact_blank.map(&:strip).uniq+ CATEGORY_FIELDS.map { |field| attributes[field] }.compact_blank.map(&:strip).uniq+ end++ # A blank category cell means "file this product under nothing", and+ # CreateCategoriesJob assigns the list wholesale to honour that. A file+ # that carries no category columns at all means something different —+ # it simply doesn't speak about categories — so without this a Shopify+ # export, or any spreadsheet that omits them, would unfile every+ # product it touched.+ def category_columns_mapped?+ cached_lookup(:category_columns_mapped) do+ import.mappings.mapped.any? { |mapping| CATEGORY_FIELDS.include?(mapping.schema_field) }+ endenddef to_spree_status(status)diff --git a/spree/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb b/spree/core/spec/presenters/spree/csv/product_variant_presenter_spec.rbindex 15b02c7782..12579349a7 100644--- a/spree/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb+++ b/spree/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb@@ -57,9 +57,7 @@ RSpec.describe Spree::CSV::ProductVariantPresenter doexpect(subject[36]).to eq nilend- context 'when index is not zero' do- let(:presenter) { described_class.new(product, variant, 1, properties, taxons, store, custom_fields) }-+ context 'when the variant has option values' dolet!(:color_option) { create(:option_type, name: 'Color', label: 'Color', products: [product]) }let!(:size_option) { create(:option_type, name: 'Size', label: 'Size', products: [product]) }@@ -68,22 +66,39 @@ RSpec.describe Spree::CSV::ProductVariantPresenter dolet(:red_color) { create(:option_value, name: 'red', label: 'Red', option_type: color_option) }let(:small_size) { create(:option_value, name: 'small', label: 'Small', option_type: size_option) }- it 'returns nil for product-level fields' do- expect(subject[2]).to be_nil # name- expect(subject[4]).to be_nil # status- expect(subject[5]).to be_nil # seller_name+ context 'when index is zero' do+ it 'returns the options alongside the product-level fields' do+ expect(subject[2]).to eq product.name+ expect(subject[1]).to eq variant.sku+ expect(subject[31]).to eq 'Color'+ expect(subject[32]).to eq 'Red'+ expect(subject[33]).to eq 'Size'+ expect(subject[34]).to eq 'Small'+ expect(subject[35]).to eq nil+ expect(subject[36]).to eq nil+ endend- it 'returns variant specific fields' do- expect(subject[1]).to eq variant.sku- expect(subject[12]).to eq variant.amount_in(store.default_currency).to_f- expect(subject[23]).to eq false- expect(subject[31]).to eq 'Color'- expect(subject[32]).to eq 'Red'- expect(subject[33]).to eq 'Size'- expect(subject[34]).to eq 'Small'- expect(subject[35]).to eq nil- expect(subject[36]).to eq nil+ context 'when index is not zero' do+ let(:presenter) { described_class.new(product, variant, 1, properties, taxons, store, custom_fields) }++ it 'returns nil for product-level fields' do+ expect(subject[2]).to be_nil # name+ expect(subject[4]).to be_nil # status+ expect(subject[5]).to be_nil # seller_name+ end++ it 'returns variant specific fields' do+ expect(subject[1]).to eq variant.sku+ expect(subject[12]).to eq variant.amount_in(store.default_currency).to_f+ expect(subject[23]).to eq false+ expect(subject[31]).to eq 'Color'+ expect(subject[32]).to eq 'Red'+ expect(subject[33]).to eq 'Size'+ expect(subject[34]).to eq 'Small'+ expect(subject[35]).to eq nil+ expect(subject[36]).to eq nil+ endendenddiff --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 14c07e88b2..0e6bb93f3f 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@@ -1,6 +1,8 @@require 'spec_helper'RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service do+ include ActiveJob::TestHelper+subject { described_class.new(row) }let(:store) { Spree::Store.default }@@ -303,6 +305,32 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service doexpect(size_option_type.option_values.find_by(label: 'XS')).to be_presentend+ context 'when an existing option value contains the imported one as a substring' do+ let(:color) { Spree::OptionType.find_by!(name: 'color') }+ let!(:black_and_red) { create(:option_value, name: 'black-and-red', label: 'Black and Red', option_type: color) }+ let!(:red) { create(:option_value, name: 'red', label: 'Red', option_type: color) }++ let(:row_data) { super().merge('option1_value' => 'Red') }++ it 'picks the exactly matching option value' do+ expect(variant.option_values).to include(red)+ expect(variant.option_values).not_to include(black_and_red)+ end++ it 'does not create a duplicate option value' do+ expect { subject.process! }.not_to change { color.option_values.count }+ end+ end++ context 'when an existing option type contains the imported one as a substring' do+ let!(:color_family) { create(:option_type, name: 'color-family', label: 'Color Family', store: store) }++ it 'picks the exactly matching option type' do+ expect(variant.option_values.map(&:option_type)).not_to include(color_family)+ expect(variant.option_values.map { |ov| ov.option_type.label }).to include('Color')+ end+ end+context 'when another store has an option type with the same name' dolet!(:foreign_fabric) { create(:option_type, name: 'fabric', label: 'Fabric', store: create(:store)) }let(:row_data) { super().merge('option3_name' => 'Fabric', 'option3_value' => 'Cotton') }@@ -317,13 +345,13 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service docontext 'when a concurrent worker has already created the option type/value' do# Simulates two Sidekiq workers racing on the same option name during a CSV import.- # Force search_by_name to miss on the initial lookup so create! runs, then return+ # Force with_name to miss on the initial lookup so create! runs, then return# real results on retry so the rescue path can find the peer's record.- # Option values don't need stubbing — they don't exist yet, so search_by_name+ # Option values don't need stubbing — they don't exist yet, so with_name# naturally misses and create! succeeds.before doseen_option_types = Set.new- allow(Spree::OptionType).to receive(:search_by_name).and_wrap_original do |original, query|+ allow(Spree::OptionType).to receive(:with_name).and_wrap_original do |original, query|seen_option_types.add?(query) ? Spree::OptionType.none : original.call(query)endend@@ -338,8 +366,8 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service docontext 'when the create! reaches the DB and the unique index rejects it' do# Mirrors the production race: validator passed (peer not committed yet) but# the INSERT collides at the DB once the peer commits. Pre-create the peer rows- # so find_by!/search_by_name can locate them on retry.- # Option values are pre-created, so search_by_name naturally finds them.+ # so find_by!/with_name can locate them on retry.+ # Option values are pre-created, so with_name naturally finds them.let!(:color_blue) { create(:option_value, name: 'Blue', label: 'Blue', option_type: Spree::OptionType.find_by(name: 'color')) }let!(:size_xs) { create(:option_value, name: 'XS', label: 'XS', option_type: Spree::OptionType.find_by(name: 'size')) }@@ -631,6 +659,36 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service doendend+ context 'when the file has no category columns' do+ let!(:category) { create(:category, store: store) }++ let(:row_data) do+ csv_row_hash(+ 'slug' => 'denim-shirt',+ 'name' => 'Denim Shirt',+ 'option1_name' => 'Color',+ 'option1_value' => 'Blue'+ )+ end++ before do+ product.categories = [category]+ import.mappings.where(schema_field: %w[category1 category2 category3]).update_all(file_column: nil)+ end++ it 'does not enqueue CreateCategoriesJob' do+ expect {+ subject.process!+ }.not_to have_enqueued_job(Spree::Imports::CreateCategoriesJob)+ end++ it 'leaves the existing categories in place' do+ perform_enqueued_jobs { subject.process! }++ expect(product.reload.categories).to contain_exactly(category)+ end+ end+context 'when importing a variant row' dolet(:row_data) docsv_row_hash(@@ -711,6 +769,79 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service doendend+ # How a Spree export and a Shopify export both write a multi-variant product:+ # the product's own attributes sit on its first variant's row.+ context 'when importing a row carrying both product attributes and options' do+ let(:row_data) do+ csv_row_hash(+ 'slug' => 'linen-shirt',+ 'sku' => 'LINEN-SHIRT-BLUE',+ 'name' => 'Linen Shirt',+ 'status' => 'active',+ 'description' => 'Breathable linen.',+ 'price' => '62.99',+ 'currency' => 'USD',+ 'product_type' => 'Apparel',+ 'option1_name' => 'Color',+ 'option1_value' => 'Blue'+ )+ end++ it 'creates the product and the option variant from the one row' do+ variant = subject.process!+ product = variant.product++ expect(product.slug).to eq 'linen-shirt'+ expect(product.name).to eq 'Linen Shirt'+ expect(product.status).to eq 'active'+ expect(product.description).to eq 'Breathable linen.'+ expect(product.product_type.name).to eq 'Apparel'++ expect(variant.sku).to eq 'LINEN-SHIRT-BLUE'+ expect(variant.option_values.map(&:label)).to eq ['Blue']+ expect(variant.price_in('USD').amount.to_f).to eq 62.99+ end++ it 'leaves no option-less placeholder variant behind' do+ product = subject.process!.product++ expect(product.variants.reload.count).to eq 1+ expect(product.default_variant.option_values.map(&:label)).to eq ['Blue']+ end++ context 'when the product already exists' do+ let!(:existing_product) { create(:product, slug: 'linen-shirt', name: 'Old Name', status: 'draft') }++ it 'updates the product-level fields' do+ product = subject.process!.product++ expect(product.id).to eq existing_product.id+ expect(product.name).to eq 'Linen Shirt'+ expect(product.status).to eq 'active'+ end+ end++ context 'followed by the product\'s remaining variant rows' do+ let(:second_row) do+ create(:import_row, import: import, data: csv_row_hash(+ 'slug' => 'linen-shirt',+ 'sku' => 'LINEN-SHIRT-RED',+ 'price' => '62.99',+ 'currency' => 'USD',+ 'option1_name' => 'Color',+ 'option1_value' => 'Red'+ ).to_json)+ end++ it 'keeps every variant' do+ product = subject.process!.product+ described_class.new(second_row).process!++ expect(product.variants.reload.flat_map { |v| v.option_values.map(&:label) }).to contain_exactly('Blue', 'Red')+ end+ end+ end+context 'when importing a variant row with options but slug is missing' dolet(:row_data) docsv_row_hash(