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.

Fixed: Importing metafields via CSV doesn't work properly for multi-v… (#13257)

Damian Legawiecc81af4c00c6f
diff --git a/core/app/services/spree/imports/row_processors/product_variant.rb b/core/app/services/spree/imports/row_processors/product_variant.rb
index b42ab9fba1..67d561affc 100644
--- a/core/app/services/spree/imports/row_processors/product_variant.rb
+++ b/core/app/services/spree/imports/row_processors/product_variant.rb
@@ -52,16 +52,26 @@ module Spree
private
def ensure_product_exists
- product = Spree::Product.new
- if attributes['slug'].present?
- existing_product = product_scope.find_by(slug: attributes['slug'].strip.downcase)
- product = existing_product if existing_product.present?
- end
+ if options.empty?
+ # For master variants, create or update the product
+ product = Spree::Product.new
+ if attributes['slug'].present?
+ existing_product = product_scope.find_by(slug: attributes['slug'].strip.downcase)
+ product = existing_product if existing_product.present?
+ end
- product = assign_attributes_to_product(product)
- product.save!
- handle_metafields(product)
- product
+ product = assign_attributes_to_product(product)
+ product.save!
+ handle_metafields(product)
+ product
+ else
+ # For non-master variants, only look up the product
+ if attributes['slug'].present?
+ product_scope.find_by!(slug: attributes['slug'].strip.downcase)
+ else
+ raise ActiveRecord::RecordNotFound, 'Product slug is required for variant rows'
+ end
+ end
end
def product_scope
diff --git a/core/spec/services/spree/imports/row_processors/product_variant_spec.rb b/core/spec/services/spree/imports/row_processors/product_variant_spec.rb
index a13240b182..d3fb11e20d 100644
--- a/core/spec/services/spree/imports/row_processors/product_variant_spec.rb
+++ b/core/spec/services/spree/imports/row_processors/product_variant_spec.rb
@@ -311,6 +311,44 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service do
end
end
+ context 'when importing a variant row with options but product does not exist' do
+ let(:row_data) do
+ csv_row_hash(
+ 'slug' => 'non-existent-shirt',
+ 'sku' => 'NON-EXISTENT-SHIRT-BLUE-XS',
+ 'price' => '62.99',
+ 'currency' => 'USD',
+ 'option1_name' => 'Color',
+ 'option1_value' => 'Blue',
+ 'option2_name' => 'Size',
+ 'option2_value' => 'XS'
+ )
+ end
+
+ it 'raises ActiveRecord::RecordNotFound' do
+ expect { subject.process! }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
+
+ context 'when importing a variant row with options but slug is missing' do
+ let(:row_data) do
+ csv_row_hash(
+ 'slug' => '',
+ 'sku' => 'MISSING-SLUG-BLUE-XS',
+ 'price' => '62.99',
+ 'currency' => 'USD',
+ 'option1_name' => 'Color',
+ 'option1_value' => 'Blue',
+ 'option2_name' => 'Size',
+ 'option2_value' => 'XS'
+ )
+ end
+
+ it 'raises ActiveRecord::RecordNotFound with descriptive message' do
+ expect { subject.process! }.to raise_error(ActiveRecord::RecordNotFound, 'Product slug is required for variant rows')
+ end
+ end
+
context 'when variant row refers to missing product slug' do
let(:row_data) do
csv_row_hash(
@@ -503,5 +541,42 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service do
end
end
end
+
+ context 'when processing a non-master variant row' do
+ let!(:existing_product) do
+ p = create(:product, slug: 'denim-shirt', name: 'Denim Shirt', stores: [store])
+ p.set_metafield('custom.brand', 'Awesome Brand')
+ p.set_metafield('custom.material', 'Cotton')
+ p
+ end
+
+ let(:row_data) do
+ csv_row_hash(
+ 'slug' => 'denim-shirt',
+ 'sku' => 'DENIM-SHIRT-BLUE-XS',
+ 'price' => '62.99',
+ 'currency' => 'USD',
+ 'option1_name' => 'Color',
+ 'option1_value' => 'Blue',
+ 'option2_name' => 'Size',
+ 'option2_value' => 'XS',
+ 'metafield.custom.brand' => '',
+ 'metafield.custom.material' => ''
+ )
+ end
+
+ it 'does not clear out existing metafield values' do
+ expect(existing_product.metafields.count).to eq 2
+
+ product = variant.product
+
+ expect(product.id).to eq existing_product.id
+ expect(product.has_metafield?('custom.brand')).to be true
+ expect(product.get_metafield('custom.brand').value).to eq 'Awesome Brand'
+ expect(product.has_metafield?('custom.material')).to be true
+ expect(product.get_metafield('custom.material').value).to eq 'Cotton'
+ expect(product.metafields.count).to eq 2
+ end
+ end
end
end