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
Fixed: CSV importer persisting shipping/tax category (#13276)
Damian Legawiec10292597bf7a
diff --git a/core/app/models/spree/import_schemas/products.rb b/core/app/models/spree/import_schemas/products.rbindex 011ad8309d..0f692c01a2 100644--- a/core/app/models/spree/import_schemas/products.rb+++ b/core/app/models/spree/import_schemas/products.rb@@ -26,7 +26,7 @@ module Spree{ name: 'inventory_count', label: 'Inventory Count' },{ name: 'inventory_backorderable', label: 'Inventory Backorderable' },{ name: 'tax_category', label: 'Tax Category' },- { name: 'digital', label: 'Digital' },+ { name: 'shipping_category', label: 'Shipping Category' },{ name: 'image1_src', label: 'Image 1 URL' },{ name: 'image2_src', label: 'Image 2 URL' },{ name: 'image3_src', label: 'Image 3 URL' },diff --git a/core/app/presenters/spree/csv/product_variant_presenter.rb b/core/app/presenters/spree/csv/product_variant_presenter.rbindex dcd0b52d1f..e45be07b57 100644--- a/core/app/presenters/spree/csv/product_variant_presenter.rb+++ b/core/app/presenters/spree/csv/product_variant_presenter.rb@@ -32,7 +32,7 @@ module Spree'inventory_count','inventory_backorderable','tax_category',- 'digital',+ 'shipping_category','image1_src','image2_src','image3_src',@@ -66,7 +66,7 @@ module Spree# For the primary variant row (when the index is zero), product-level details such as name,# slug, status, vendor and brand names, description, meta tags, and tag/label lists are included.# In all cases, variant-specific attributes (e.g., id, SKU, pricing, dimensions, weight,- # availability dates, inventory count, digital flag, tax category, image URLs via original_url,+ # availability dates, inventory count, shipping category, tax category, image URLs via original_url,# and the first three option types and corresponding option values) are appended.# Additionally, when the index is zero, associated taxons and properties are added.#@@ -103,7 +103,7 @@ module Spreetotal_on_hand == BigDecimal::INFINITY ? '∞' : total_on_hand,variant.backorderable?,variant.tax_category&.name,- variant.digital?,+ product.shipping_category&.name,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),diff --git a/core/app/services/spree/imports/row_processors/product_variant.rb b/core/app/services/spree/imports/row_processors/product_variant.rbindex 67d561affc..e37c9f4d76 100644--- a/core/app/services/spree/imports/row_processors/product_variant.rb+++ b/core/app/services/spree/imports/row_processors/product_variant.rb@@ -33,6 +33,12 @@ module Spreevariant.depth = attributes['depth'] if attributes['depth'].present?variant.track_inventory = attributes['track_inventory'] if attributes['track_inventory'].present?variant.option_value_variants = prepare_option_value_variants++ if attributes['tax_category'].present?+ tax_category = prepare_tax_category+ variant.tax_category = tax_category if tax_category.present?+ end+variant.save!if attributes['price'].present?@@ -94,10 +100,27 @@ module Spreeproduct.status = to_spree_status(attributes['status']) if attributes['status'].present?product.tag_list = attributes['tags'] if attributes['tags'].present?- product.taxons = prepare_taxons if options.empty?+ if options.empty?+ if attributes['shipping_category'].present?+ shipping_category = prepare_shipping_category+ product.shipping_category = shipping_category if shipping_category.present?+ end+ product.taxons = prepare_taxons+ end+productend+ def prepare_shipping_category+ shipping_category_name = attributes['shipping_category'].strip+ Spree::ShippingCategory.find_by(name: shipping_category_name)+ end++ def prepare_tax_category+ tax_category_name = attributes['tax_category'].strip+ Spree::TaxCategory.find_by(name: tax_category_name)+ end+def prepare_taxonstaxon_pretty_names = [attributes['category1'],diff --git a/core/spec/models/spree/exports/products_spec.rb b/core/spec/models/spree/exports/products_spec.rbindex 2deacc53aa..0c39e3359d 100644--- a/core/spec/models/spree/exports/products_spec.rb+++ b/core/spec/models/spree/exports/products_spec.rb@@ -61,7 +61,7 @@ RSpec.describe Spree::Exports::Products, type: :model do'inventory_count','inventory_backorderable','tax_category',- 'digital',+ 'shipping_category','image1_src','image2_src','image3_src',@@ -115,7 +115,7 @@ RSpec.describe Spree::Exports::Products, type: :model do'inventory_count','inventory_backorderable','tax_category',- 'digital',+ 'shipping_category','image1_src','image2_src','image3_src',@@ -172,7 +172,7 @@ RSpec.describe Spree::Exports::Products, type: :model do'inventory_count','inventory_backorderable','tax_category',- 'digital',+ 'shipping_category','image1_src','image2_src','image3_src',diff --git a/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb b/core/spec/presenters/spree/csv/product_variant_presenter_spec.rbindex 3edf9a5a51..5f3920e16e 100644--- a/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb+++ b/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb@@ -43,7 +43,7 @@ RSpec.describe Spree::CSV::ProductVariantPresenter doexpect(subject[25]).to eq(variant.total_on_hand == BigDecimal::INFINITY ? '∞' : variant.total_on_hand)expect(subject[26]).to eq variant.backorderable?expect(subject[27]).to eq variant.tax_category&.name- expect(subject[28]).to eq variant.digital?+ expect(subject[28]).to eq product.shipping_category&.nameexpect(subject[29]).to end_with(variant.images[0].filename.to_s)expect(subject[30]).to end_with(variant.images[1].filename.to_s)expect(subject[31]).to end_with(variant.images[2].filename.to_s)@@ -179,4 +179,26 @@ RSpec.describe Spree::CSV::ProductVariantPresenter doendendend++ describe 'shipping_category' do+ context 'when product has shipping category' do+ let(:shipping_category) { create(:shipping_category, name: 'Digital') }+ let(:product) { create(:product, stores: [store], shipping_category: shipping_category) }++ it 'exports shipping category name' do+ result = presenter.call+ expect(result[28]).to eq 'Digital'+ end+ end++ context 'when product has no shipping category explicitly set' do+ let(:product) { create(:product, stores: [store]) }++ it 'exports the assigned shipping category' do+ result = presenter.call+ expect(result[28]).to be_present+ expect(result[28]).to eq product.shipping_category.name+ end+ end+ endenddiff --git a/core/spec/services/spree/imports/row_processors/product_variant_spec.rb b/core/spec/services/spree/imports/row_processors/product_variant_spec.rbindex 35c6831eac..03898cc0cd 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@@ -11,6 +11,9 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service dobefore doimport.create_mappings+ # Manually map fields since there's no CSV file attached+ import.mappings.find_by(schema_field: 'shipping_category')&.update(file_column: 'shipping_category')+ import.mappings.find_by(schema_field: 'tax_category')&.update(file_column: 'tax_category')end# Matches how our production import will pass attributes@@ -624,4 +627,199 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service doendendend++ context 'when importing with shipping_category' do+ context 'when shipping_category exists' do+ let!(:digital_category) { create(:shipping_category, name: 'Digital') }++ let(:row_data) do+ csv_row_hash(+ 'slug' => 'digital-product',+ 'name' => 'Digital Product',+ 'status' => 'active',+ 'price' => '29.99',+ 'currency' => 'USD',+ 'shipping_category' => 'Digital'+ )+ end++ it 'assigns the shipping category to the product' do+ product = variant.product+ expect(product.shipping_category).to eq digital_category+ end+ end++ context 'when shipping_category does not exist' do+ let(:row_data) do+ csv_row_hash(+ 'slug' => 'digital-product',+ 'name' => 'Digital Product',+ 'status' => 'active',+ 'price' => '29.99',+ 'currency' => 'USD',+ 'shipping_category' => 'NonExistent'+ )+ end++ it 'assigns the default shipping category' do+ product = variant.product+ expect(product.shipping_category).to be_present+ expect(product.shipping_category.name).to eq 'Default'+ end+ end++ context 'when updating product with different shipping_category' do+ let!(:standard_category) { create(:shipping_category, name: 'Standard') }+ let!(:digital_category) { create(:shipping_category, name: 'Digital') }+ let!(:existing_product) do+ create(:product, slug: 'product-to-update', name: 'Product', stores: [store], shipping_category: standard_category)+ end++ let(:row_data) do+ csv_row_hash(+ 'slug' => 'product-to-update',+ 'name' => 'Product',+ 'status' => 'active',+ 'price' => '29.99',+ 'currency' => 'USD',+ 'shipping_category' => 'Digital'+ )+ end++ it 'updates the shipping category' do+ product = variant.product++ expect(product.id).to eq existing_product.id+ expect(product.shipping_category).to eq digital_category+ end+ end++ context 'when shipping_category is not provided' do+ let(:row_data) do+ csv_row_hash(+ 'slug' => 'product-no-category',+ 'name' => 'Product',+ 'status' => 'active',+ 'price' => '29.99',+ 'currency' => 'USD'+ )+ end++ it 'assigns the default shipping category' do+ product = variant.product++ expect(product.shipping_category).to be_present+ expect(product.shipping_category.name).to eq 'Default'+ end+ end+ end++ context 'when importing with tax_category' do+ context 'when tax_category exists' do+ let!(:clothing_tax) { create(:tax_category, name: 'Clothing') }++ let(:row_data) do+ csv_row_hash(+ 'slug' => 'test-product',+ 'name' => 'Test Product',+ 'status' => 'active',+ 'price' => '29.99',+ 'currency' => 'USD',+ 'tax_category' => 'Clothing'+ )+ end++ it 'assigns the tax category to the variant' do+ expect(variant.tax_category).to eq clothing_tax+ end+ end++ context 'when tax_category does not exist' do+ let(:row_data) do+ csv_row_hash(+ 'slug' => 'test-product',+ 'name' => 'Test Product',+ 'status' => 'active',+ 'price' => '29.99',+ 'currency' => 'USD',+ 'tax_category' => 'NonExistent'+ )+ end++ it 'does not assign a tax category' do+ expect(variant.tax_category).to be_nil+ end+ end++ context 'when updating variant with different tax_category' do+ let!(:standard_tax) { create(:tax_category, name: 'Standard') }+ let!(:clothing_tax) { create(:tax_category, name: 'Clothing') }+ let!(:existing_product) do+ p = create(:product, slug: 'product-to-update', name: 'Product', stores: [store])+ p.master.update(tax_category: standard_tax)+ p+ end++ let(:row_data) do+ csv_row_hash(+ 'slug' => 'product-to-update',+ 'name' => 'Product',+ 'status' => 'active',+ 'price' => '29.99',+ 'currency' => 'USD',+ 'tax_category' => 'Clothing'+ )+ end++ it 'updates the tax category' do+ product = variant.product++ expect(product.id).to eq existing_product.id+ expect(variant.tax_category).to eq clothing_tax+ end+ end++ context 'when tax_category is not provided' do+ let(:row_data) do+ csv_row_hash(+ 'slug' => 'product-no-tax',+ 'name' => 'Product',+ 'status' => 'active',+ 'price' => '29.99',+ 'currency' => 'USD'+ )+ end++ it 'does not assign a tax category' do+ expect(variant.tax_category).to be_nil+ end+ end++ context 'when importing a non-master variant with tax_category' do+ let!(:clothing_tax) { create(:tax_category, name: 'Clothing') }+ let!(:product) do+ p = create(:product, slug: 'denim-shirt', name: 'Denim Shirt', stores: [store])+ color = create(:option_type, name: 'color', presentation: 'Color')+ p.option_types << color+ p+ end++ let(:row_data) do+ csv_row_hash(+ 'slug' => 'denim-shirt',+ 'sku' => 'DENIM-SHIRT-BLUE',+ 'price' => '62.99',+ 'currency' => 'USD',+ 'tax_category' => 'Clothing',+ 'option1_name' => 'Color',+ 'option1_value' => 'Blue'+ )+ end++ it 'assigns tax category to the non-master variant' do+ expect(variant.is_master?).to be false+ expect(variant.tax_category).to eq clothing_tax+ end+ end+ endend