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 `Product#to_csv` when product has multiple variants

Damian Legawiec688ff6b189ac
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 9f4bfc0492..51853f077d 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -469,11 +469,15 @@ module Spree
end
taxons_for_csv ||= taxons.reorder(depth: :desc).first(3).pluck(:pretty_name)
csv_lines = []
- variants_including_master.each_with_index do |variant, index|
- next if variant.is_master? && has_variants?
- csv_lines << Spree::CSV::ProductVariantPresenter.new(self, variant, index, properties_for_csv, taxons_for_csv, store).call
+ if has_variants?
+ variants.each_with_index do |variant, index|
+ csv_lines << Spree::CSV::ProductVariantPresenter.new(self, variant, index, properties_for_csv, taxons_for_csv, store).call
+ end
+ else
+ csv_lines << Spree::CSV::ProductVariantPresenter.new(self, master, 0, properties_for_csv, taxons_for_csv, store).call
end
+
csv_lines
end
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index 183e830ab6..99b737e795 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -1060,4 +1060,59 @@ describe Spree::Product, type: :model do
end
end
end
+
+ describe '#to_csv' do
+ let(:store) { create(:store) }
+ let(:product) { create(:product, stores: [store]) }
+ let(:property) { create(:property, name: 'my-property', position: 1) }
+ let(:product_property) { create(:product_property, property: property, product: product, value: 'MyValue') }
+ let(:taxon) { create(:taxon, name: 'My Taxon') }
+
+ before do
+ product_property
+ product.taxons << taxon
+ end
+
+ context 'when product has no variants' do
+ it 'returns an array with one line of CSV data' do
+ csv_lines = product.to_csv(store)
+ expect(csv_lines.size).to eq(1)
+
+ csv_line = csv_lines.first
+ expect(csv_line).to include(product.name)
+ expect(csv_line).to include(product.master.sku)
+ expect(csv_line).to include('my-property')
+ expect(csv_line).to include('MyValue')
+ expect(csv_line).to include(taxon.pretty_name)
+ end
+ end
+
+ context 'when product has variants' do
+ let!(:variant1) { create(:variant, product: product) }
+ let!(:variant2) { create(:variant, product: product) }
+
+ it 'returns an array with CSV data for each variant' do
+ csv_lines = product.to_csv(store)
+ expect(csv_lines.size).to eq(2)
+
+ expect(csv_lines.first).to include(product.name)
+ expect(csv_lines.first).to include(variant1.sku)
+ expect(csv_lines.second).not_to include(product.name)
+ expect(csv_lines.second).to include(variant2.sku)
+ end
+ end
+
+ context 'when store is not provided' do
+ it 'uses default store' do
+ allow(product.stores).to receive(:default).and_return(store)
+ expect(product.to_csv).to be_present
+ end
+
+ it 'falls back to first store if no default' do
+ allow(product.stores).to receive(:default).and_return(nil)
+ allow(product.stores).to receive(:first).and_return(store)
+ expect(product.to_csv).to be_present
+ end
+ end
+ end
end