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
Product duplicator fixes: copy over backorderable, always set status to draft (#12213)
Łukasz Adamczyke769d219daa6
diff --git a/core/app/services/spree/products/duplicator.rb b/core/app/services/spree/products/duplicator.rbindex dc2efc28b5..1c5f05d205 100644--- a/core/app/services/spree/products/duplicator.rb+++ b/core/app/services/spree/products/duplicator.rb@@ -27,6 +27,7 @@ module Spreet.slug = nilend+ new_product.status = :draftnew_product.name = "COPY OF #{product.name}"new_product.taxons = product.taxonsnew_product.stores = product.stores@@ -44,6 +45,7 @@ module Spreenew_master.sku = sku_generator(master.sku)new_master.deleted_at = nilnew_master.prices = duplicate_prices(master.prices)+ new_master.stock_items = duplicate_stock_items(master.stock_items)master.images.each { |image| duplicate_image(image, new_master) } if include_imagesend@@ -55,6 +57,7 @@ module Spreenew_variant.deleted_at = nilnew_variant.option_values = variant.option_values.map { |option_value| option_value }new_variant.prices = duplicate_prices(variant.prices)+ new_variant.stock_items = duplicate_stock_items(variant.stock_items)variant.images.each { |image| duplicate_image(image, new_variant) } if include_images@@ -70,6 +73,14 @@ module Spreeendend+ def duplicate_stock_items(stock_items)+ stock_items.map do |stock_item|+ new_stock_item = stock_item.dup+ new_stock_item.count_on_hand = 0+ new_stock_item+ end+ end+def duplicate_image(image, viewable)new_image = image.dupnew_image.viewable = viewablediff --git a/core/spec/services/spree/products/duplicator_spec.rb b/core/spec/services/spree/products/duplicator_spec.rbindex 7eaf6c7f46..71cec17eca 100644--- a/core/spec/services/spree/products/duplicator_spec.rb+++ b/core/spec/services/spree/products/duplicator_spec.rb@@ -6,7 +6,7 @@ RSpec.describe Spree::Products::Duplicator dolet(:store) { create(:store) }let!(:product_property) { create(:product_property, product: product) }- let!(:product) { create(:product, stores: [store], tag_list: ['tag1', 'tag2']) }+ let!(:product) { create(:product, stores: [store], tag_list: ['tag1', 'tag2'], status: :active) }let(:filepath) { File.expand_path('../../../fixtures/thinking-cat.jpg', __dir__) }let(:master_image_params) do@@ -23,6 +23,9 @@ RSpec.describe Spree::Products::Duplicator donew_image = Spree::Image.new(master_image_params)new_image.attachment.attach(io: file, filename: File.basename(file))new_image.save!++ product.master.update!(barcode: '1234567890')+ product.master.stock_items.last.update!(count_on_hand: 100, backorderable: true)endit { is_expected.to be_success }@@ -54,6 +57,10 @@ RSpec.describe Spree::Products::Duplicator dodescribe 'product attributes' dolet!(:new_product) { duplicate.value }+ it 'always sets the product as draft' do+ expect(new_product).to be_draft+ end+it 'sets an unique name' doexpect(new_product.name).to eql "COPY OF #{product.name}"end@@ -70,6 +77,15 @@ RSpec.describe Spree::Products::Duplicator doit 'copies tags' doexpect(new_product.tag_list).to eq(['tag1', 'tag2'])end++ it 'clones barcode' do+ expect(new_product.barcode).to eq('1234567890')+ end++ it 'clones backorderable and sets stock to 0' do+ expect(new_product.master).to be_backorderable+ expect(new_product.master.total_on_hand).to eq(0)+ endenddescribe 'product prices' do@@ -107,13 +123,14 @@ RSpec.describe Spree::Products::Duplicator dolet(:option_value1) { create(:option_value, name: 'OptionValue1', option_type: option_type) }let(:option_value2) { create(:option_value, name: 'OptionValue2', option_type: option_type) }- let!(:variant1) { create(:variant, product: product, price: 24.99, compare_at_price: 25.99, option_values: [option_value1]) }- let!(:variant2) { create(:variant, product: product, price: 29.99, compare_at_price: 30.99, option_values: [option_value2]) }+ let!(:variant1) { create(:variant, product: product, option_values: [option_value1], barcode: 'v1-123') }+ let!(:variant2) { create(:variant, product: product, option_values: [option_value2], barcode: 'v2-456') }let(:new_product) { duplicate.value }before do- product.master.update!(compare_at_price: 20.99)+ variant1.stock_items.last.update!(count_on_hand: 100, backorderable: true)+ variant2.stock_items.last.update!(count_on_hand: 200, backorderable: false)endit 'duplicates the variants' do@@ -125,6 +142,20 @@ RSpec.describe Spree::Products::Duplicator doexpect { duplicate }.to change { Spree::OptionValue.count }.by(0)end+ it 'clones barcodes' do+ expect(new_product.variants.pluck(:barcode)).to contain_exactly('v1-123', 'v2-456')+ end++ it 'clones backorderable and sets stock to 0' do+ variant1_copy = new_product.variants.find_by!(barcode: 'v1-123')+ expect(variant1_copy).to be_backorderable+ expect(variant1_copy.total_on_hand).to eq(0)++ variant2_copy = new_product.variants.find_by!(barcode: 'v2-456')+ expect(variant2_copy).not_to be_backorderable+ expect(variant2_copy.total_on_hand).to eq(0)+ end+describe 'image duplication' dolet(:variant1_image_params) do{