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.

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.mdx
index 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.rb
index 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 Spree
end
end
+ scope :with_name, ->(value) { where(name: value.to_s.parameterize) }
+
def set_name_from_label
self.name = label
end
diff --git a/spree/core/app/presenters/spree/csv/product_variant_presenter.rb b/spree/core/app/presenters/spree/csv/product_variant_presenter.rb
index 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 Spree
spree_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.rb
index 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 Spree
module RowProcessors
class ProductVariant < Base
OPTION_TYPES_COUNT = 3
+ CATEGORY_FIELDS = %w[category1 category2 category3].freeze
def initialize(row, **)
super
@@ -91,9 +92,17 @@ module Spree
product.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.new
if attributes['slug'].present?
existing_product = product_scope.find_by(slug: attributes['slug'].strip.downcase)
@@ -167,9 +176,12 @@ module Spree
end
def assign_attributes_to_product(product)
- # set the SKU on a product/header row so process! updates the default variant instead of creating a new one
if 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 Spree
product.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?
end
product
@@ -304,11 +314,11 @@ module Spree
def find_or_create_option_type!(label)
cached_lookup(:option_type, label) do
begin
- 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 => e
raise unless uniqueness_conflict?(e, :name)
- store_option_types.search_by_name(label).first!
+ store_option_types.with_name(label).first!
end
end
end
@@ -320,11 +330,11 @@ module Spree
def find_or_create_option_value!(option_type, label)
cached_lookup(:option_value, option_type.id, label) do
begin
- 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 => e
raise unless uniqueness_conflict?(e, :name)
- option_type.option_values.search_by_name(label).first!
+ option_type.option_values.with_name(label).first!
end
end
end
@@ -375,7 +385,7 @@ module Spree
end
def 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? }
end
def 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_names
return Spree::Imports::CreateCategoriesJob.perform_now(product.id, store.id, names) if import.preferred_inline
@@ -450,11 +461,19 @@ module Spree
end
def 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) }
+ end
end
def 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.rb
index 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 do
expect(subject[36]).to eq nil
end
- 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' do
let!(: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 do
let(: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
+ end
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
+ 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
+ end
end
end
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 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 do
expect(size_option_type.option_values.find_by(label: 'XS')).to be_present
end
+ 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' do
let!(: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 do
context '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 do
seen_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)
end
end
@@ -338,8 +366,8 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service do
context '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 do
end
end
+ 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' do
let(:row_data) do
csv_row_hash(
@@ -711,6 +769,79 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service do
end
end
+ # 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' do
let(:row_data) do
csv_row_hash(