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
Fix exporting products and variants (#12550)
Łukasz Adamczyk0181a988f50e
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rbindex 2759b81641..e656851cf6 100644--- a/core/app/models/spree/product.rb+++ b/core/app/models/spree/product.rb@@ -650,7 +650,7 @@ module Spreecsv_lines = []if has_variants?- variants.each_with_index do |variant, index|+ variants_including_master.each_with_index do |variant, index|csv_lines << Spree::CSV::ProductVariantPresenter.new(self, variant, index, properties_for_csv, taxons_for_csv, store).callendelsediff --git a/core/app/presenters/spree/csv/product_variant_presenter.rb b/core/app/presenters/spree/csv/product_variant_presenter.rbindex 6a2237d791..85092c1bce 100644--- a/core/app/presenters/spree/csv/product_variant_presenter.rb+++ b/core/app/presenters/spree/csv/product_variant_presenter.rb@@ -3,6 +3,7 @@ module Spreeclass ProductVariantPresenterCSV_HEADERS = ['product_id',+ 'sku','name','slug','status',@@ -14,8 +15,6 @@ module Spree'meta_keywords','tags','labels',- 'variant_id',- 'variant_sku','price','compare_at_price','currency',@@ -27,6 +26,7 @@ module Spree'weight_unit','available_on','discontinue_on',+ 'track_inventory','inventory_count','inventory_backorderable','tax_category',@@ -71,6 +71,7 @@ module Spreedef callcsv = [product.id,+ variant.sku,index.zero? ? product.name : nil,index.zero? ? product.slug : nil,index.zero? ? product.status : nil,@@ -82,8 +83,6 @@ module Spreeindex.zero? ? product.meta_keywords : nil,index.zero? ? product.tag_list.to_s : nil,index.zero? ? product.label_list.to_s : nil,- variant.id,- variant.sku,variant.amount_in(currency).to_f,variant.compare_at_price&.to_f,currency,@@ -94,7 +93,8 @@ module Spreevariant.weight,variant.weight_unit,variant.available_on&.strftime('%Y-%m-%d %H:%M:%S'),- variant.discontinue_on&.strftime('%Y-%m-%d %H:%M:%S'),+ (variant.discontinue_on || product.discontinue_on)&.strftime('%Y-%m-%d %H:%M:%S'),+ variant.track_inventory?,variant.total_on_hand == BigDecimal::INFINITY ? '∞' : variant.total_on_hand,variant.backorderable?,variant.tax_category&.name,@@ -102,12 +102,12 @@ module Spreevariant.images[0]&.original_url,variant.images[1]&.original_url,variant.images[2]&.original_url,- option_type(0)&.name,- option_value(option_type(0)),- option_type(1)&.name,- option_value(option_type(1)),- option_type(2)&.name,- option_value(option_type(2))+ index.positive? ? option_type(0)&.presentation : nil,+ index.positive? ? option_value(option_type(0)) : nil,+ index.positive? ? option_type(1)&.presentation : nil,+ index.positive? ? option_value(option_type(1)) : nil,+ index.positive? ? option_type(2)&.presentation : nil,+ index.positive? ? option_value(option_type(2)) : nil]if index.zero?@@ -123,7 +123,7 @@ module Spreeenddef option_value(option_type)- variant.option_values.find { |ov| ov.option_type == option_type }&.name+ variant.option_values.find { |ov| ov.option_type == option_type }&.presentationendendenddiff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rbindex 8282951664..e86998d71d 100644--- a/core/spec/models/spree/product_spec.rb+++ b/core/spec/models/spree/product_spec.rb@@ -1155,14 +1155,22 @@ describe Spree::Product, type: :model dolet!(:variant1) { create(:variant, product: product) }let!(:variant2) { create(:variant, product: product) }- it 'returns an array with CSV data for each variant' do+ before do+ product.master.update!(sku: 'test-product-master-sku')+ end++ it 'returns an array with CSV data for each variant including the master variant' docsv_lines = product.reload.to_csv(store)- expect(csv_lines.size).to eq(2)+ expect(csv_lines.size).to eq(3)++ expect(csv_lines[0]).to include(product.name)+ expect(csv_lines[0]).to include('test-product-master-sku')++ expect(csv_lines[1]).not_to include(product.name)+ expect(csv_lines[1]).to include(variant1.sku)- 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)+ expect(csv_lines[2]).not_to include(product.name)+ expect(csv_lines[2]).to include(variant2.sku)endenddiff --git a/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb b/core/spec/presenters/spree/csv/product_variant_presenter_spec.rbindex 64ccf15bb5..fae88645ad 100644--- a/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb+++ b/core/spec/presenters/spree/csv/product_variant_presenter_spec.rb@@ -15,30 +15,30 @@ RSpec.describe Spree::CSV::ProductVariantPresenter doit 'returns array with correct values' doexpect(subject[0]).to eq product.id- expect(subject[1]).to eq product.name- expect(subject[2]).to eq product.slug- expect(subject[3]).to eq product.status- expect(subject[4]).to eq product.try(:vendor_name)- expect(subject[5]).to eq product.brand_name- expect(subject[6]).to eq product.description&.html_safe- expect(subject[7]).to eq product.meta_title- expect(subject[8]).to eq product.meta_description- expect(subject[9]).to eq product.meta_keywords- expect(subject[10]).to eq product.tag_list.to_s- expect(subject[11]).to eq product.label_list.to_s- expect(subject[12]).to eq variant.id- expect(subject[13]).to eq variant.sku- expect(subject[14]).to eq variant.amount_in(store.default_currency).to_f- expect(subject[15]).to eq variant.compare_at_price&.to_f- expect(subject[16]).to eq store.default_currency- expect(subject[17]).to eq variant.width- expect(subject[18]).to eq variant.height- expect(subject[19]).to eq variant.depth- expect(subject[20]).to eq 'in'- expect(subject[21]).to eq variant.weight- expect(subject[22]).to eq 'lb'- expect(subject[23]).to eq variant.available_on&.strftime('%Y-%m-%d %H:%M:%S')- expect(subject[24]).to eq variant.discontinue_on&.strftime('%Y-%m-%d %H:%M:%S')+ expect(subject[1]).to eq variant.sku+ expect(subject[2]).to eq product.name+ expect(subject[3]).to eq product.slug+ expect(subject[4]).to eq product.status+ expect(subject[5]).to eq product.try(:vendor_name)+ expect(subject[6]).to eq product.brand_name+ expect(subject[7]).to eq product.description&.html_safe+ expect(subject[8]).to eq product.meta_title+ expect(subject[9]).to eq product.meta_description+ expect(subject[10]).to eq product.meta_keywords+ expect(subject[11]).to eq product.tag_list.to_s+ expect(subject[12]).to eq product.label_list.to_s+ expect(subject[13]).to eq variant.amount_in(store.default_currency).to_f+ expect(subject[14]).to eq variant.compare_at_price&.to_f+ expect(subject[15]).to eq store.default_currency+ expect(subject[16]).to eq variant.width+ expect(subject[17]).to eq variant.height+ expect(subject[18]).to eq variant.depth+ expect(subject[19]).to eq 'in'+ expect(subject[20]).to eq variant.weight+ expect(subject[21]).to eq 'lb'+ expect(subject[22]).to eq variant.available_on&.strftime('%Y-%m-%d %H:%M:%S')+ expect(subject[23]).to eq variant.discontinue_on&.strftime('%Y-%m-%d %H:%M:%S')+ expect(subject[24]).to eq variant.track_inventory?expect(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@@ -46,29 +46,49 @@ RSpec.describe Spree::CSV::ProductVariantPresenter doexpect(subject[29]).to eq variant.images[0].original_urlexpect(subject[30]).to eq variant.images[1].original_urlexpect(subject[31]).to eq variant.images[2].original_url+ expect(subject[32]).to eq nil+ expect(subject[33]).to eq nil+ expect(subject[34]).to eq nil+ expect(subject[35]).to eq nil+ expect(subject[36]).to eq nil+ expect(subject[37]).to eq nilendcontext 'when index is not zero' do- let(:variant) { create(:variant, product: product) }let(:presenter) { described_class.new(product, variant, 1, properties, taxons, store) }+ let!(:color_option) { create(:option_type, name: 'Color', presentation: 'Color', products: [product]) }+ let!(:size_option) { create(:option_type, name: 'Size', presentation: 'Size', products: [product]) }++ let(:variant) { create(:variant, product: product, track_inventory: false, option_values: [red_color, small_size]) }++ let(:red_color) { create(:option_value, name: 'red', presentation: 'Red', option_type: color_option) }+ let(:small_size) { create(:option_value, name: 'small', presentation: 'Small', option_type: size_option) }+it 'returns nil for product-level fields' do- expect(subject[1]).to be_nil # name- expect(subject[2]).to be_nil # slug- expect(subject[3]).to be_nil # status- expect(subject[4]).to be_nil # vendor_name+ expect(subject[2]).to be_nil # name+ expect(subject[3]).to be_nil # slug+ expect(subject[4]).to be_nil # status+ expect(subject[5]).to be_nil # vendor_nameendit 'returns variant specific fields' do- expect(subject[12]).to eq variant.id- expect(subject[13]).to eq variant.sku- expect(subject[14]).to eq variant.amount_in(store.default_currency).to_f+ expect(subject[1]).to eq variant.sku+ expect(subject[13]).to eq variant.amount_in(store.default_currency).to_f+ expect(subject[24]).to eq false+ expect(subject[32]).to eq 'Color'+ expect(subject[33]).to eq 'Red'+ expect(subject[34]).to eq 'Size'+ expect(subject[35]).to eq 'Small'+ expect(subject[36]).to eq nil+ expect(subject[37]).to eq nilendendenddescribe '#option_type' dolet(:option_type) { create(:option_type) }+before { product.option_types << option_type }it 'returns option type at given index' do@@ -89,8 +109,8 @@ RSpec.describe Spree::CSV::ProductVariantPresenter dovariant.option_values << option_valueend- it 'returns option value name for given option type' do- expect(presenter.option_value(option_type)).to eq option_value.name+ it 'returns option value presentation for given option type' do+ expect(presenter.option_value(option_type)).to eq option_value.presentationendit 'returns nil for option type without value' do