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: product properties, variant prices/compare at prices (#12206)
Łukasz Adamczykf4f68a0df7c5
diff --git a/core/app/services/spree/products/duplicator.rb b/core/app/services/spree/products/duplicator.rbindex b00d47f9c6..dc2efc28b5 100644--- a/core/app/services/spree/products/duplicator.rb+++ b/core/app/services/spree/products/duplicator.rb@@ -13,6 +13,8 @@ module Spreenew_product.send(:duplicate_extra, product) if new_product.respond_to?(:duplicate_extra)new_product.save+ new_product.product_properties = duplicate_properties(product.product_properties) if new_product.persisted?+new_product.persisted? ? success(new_product) : failure(new_product, duplicate_error_message(new_product))end@@ -31,10 +33,9 @@ module Spreenew_product.created_at = nilnew_product.deleted_at = nilnew_product.updated_at = nil+ new_product.tag_list = product.tag_listnew_product.master = duplicate_master(product.master, include_images)- new_product.variants = product.variants.map { |variant| duplicate_variant(variant) }-- duplicate_properties(product.product_properties, new_product)+ new_product.variants = product.variants.map { |variant| duplicate_variant(variant, include_images) }endend@@ -42,21 +43,33 @@ module Spreemaster.dup.tap do |new_master|new_master.sku = sku_generator(master.sku)new_master.deleted_at = nil- new_master.price = master.price- new_master.currency = master.currency+ new_master.prices = duplicate_prices(master.prices)master.images.each { |image| duplicate_image(image, new_master) } if include_imagesendend- def duplicate_variant(variant)+ def duplicate_variant(variant, include_images)new_variant = variant.dupnew_variant.sku = sku_generator(new_variant.sku)new_variant.deleted_at = nilnew_variant.option_values = variant.option_values.map { |option_value| option_value }+ new_variant.prices = duplicate_prices(variant.prices)++ variant.images.each { |image| duplicate_image(image, new_variant) } if include_images+new_variantend+ def duplicate_prices(prices)+ prices.map do |price|+ new_price = price.dup+ new_price.created_at = nil+ new_price.updated_at = nil+ new_price+ end+ end+def duplicate_image(image, viewable)new_image = image.dupnew_image.viewable = viewable@@ -65,13 +78,13 @@ module Spreenew_imageend- def duplicate_properties(product_properties, new_product)- product_properties.each do |prop|+ def duplicate_properties(product_properties)+ product_properties.map do |prop|new_prop = prop.dup+ new_prop.product = nilnew_prop.created_at = nilnew_prop.updated_at = nil- new_prop.product = new_product- new_prop.save+ new_propendenddiff --git a/core/spec/services/spree/products/duplicator_spec.rb b/core/spec/services/spree/products/duplicator_spec.rbindex a5823d6a46..7eaf6c7f46 100644--- a/core/spec/services/spree/products/duplicator_spec.rb+++ b/core/spec/services/spree/products/duplicator_spec.rb@@ -6,10 +6,10 @@ RSpec.describe Spree::Products::Duplicator dolet(:store) { create(:store) }let!(:product_property) { create(:product_property, product: product) }- let!(:product) { create(:product, stores: [store]) }+ let!(:product) { create(:product, stores: [store], tag_list: ['tag1', 'tag2']) }- let(:file) { File.open(File.expand_path('../../../fixtures/thinking-cat.jpg', __dir__)) }- let(:params) do+ let(:filepath) { File.expand_path('../../../fixtures/thinking-cat.jpg', __dir__) }+ let(:master_image_params) do{viewable_id: product.master.id,viewable_type: 'Spree::Variant',@@ -19,7 +19,8 @@ RSpec.describe Spree::Products::Duplicator doendbefore do- new_image = Spree::Image.new(params)+ file = File.open(filepath)+ new_image = Spree::Image.new(master_image_params)new_image.attachment.attach(io: file, filename: File.basename(file))new_image.save!end@@ -65,6 +66,34 @@ RSpec.describe Spree::Products::Duplicator doexpect(new_product.product_properties.count).to eq 1expect(new_product.product_properties.first.property.name).to eql product_property.property.nameend++ it 'copies tags' do+ expect(new_product.tag_list).to eq(['tag1', 'tag2'])+ end+ end++ describe 'product prices' do+ let(:new_product) { duplicate.value }++ before do+ product.master.prices.destroy_all+ product.master.prices = [+ create(:price, amount: 10.99, compare_at_amount: 11.99, currency: 'USD'),+ create(:price, amount: 8.99, compare_at_amount: 9.99, currency: 'GBP')+ ]+ end++ it 'clones prices' do+ expect(duplicate).to be_success++ usd_price = new_product.master.price_in('USD')+ expect(usd_price.amount).to eq(10.99)+ expect(usd_price.compare_at_amount).to eq(11.99)++ gbp_price = new_product.master.price_in('GBP')+ expect(gbp_price.amount).to eq(8.99)+ expect(gbp_price.compare_at_amount).to eq(9.99)+ endenddescribe 'stores' do@@ -78,8 +107,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, option_values: [option_value1]) }- let!(:variant2) { create(:variant, product: product, option_values: [option_value2]) }+ 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(:new_product) { duplicate.value }++ before do+ product.master.update!(compare_at_price: 20.99)+ endit 'duplicates the variants' do# will change the count by 3, since there will be a master variant as well@@ -89,5 +124,87 @@ RSpec.describe Spree::Products::Duplicator doit 'doesn\'t duplicate the option values' doexpect { duplicate }.to change { Spree::OptionValue.count }.by(0)end++ describe 'image duplication' do+ let(:variant1_image_params) do+ {+ viewable_id: variant1.id,+ viewable_type: 'Spree::Variant',+ alt: 'position 1',+ position: 1+ }+ end++ let(:variant2_image_params) do+ {+ viewable_id: variant2.id,+ viewable_type: 'Spree::Variant',+ alt: 'position 2',+ position: 2+ }+ end++ before do+ file = File.open(filepath)+ variant1_image = Spree::Image.new(variant1_image_params)+ variant1_image.attachment.attach(io: file, filename: File.basename(file))+ variant1_image.save!++ file = File.open(filepath)+ variant2_image = Spree::Image.new(variant2_image_params)+ variant2_image.attachment.attach(io: file, filename: File.basename(file))+ variant2_image.save!+ end++ it 'clones images by default' do+ expect { duplicate }.to change(Spree::Image, :count).by(3)+ end++ context 'when excluding images from cloning' do+ subject(:duplicate) { described_class.call(product: product, include_images: false) }++ it 'doesn\'t clone images' do+ expect { duplicate }.not_to change(Spree::Image, :count)+ end+ end+ end++ describe 'variant prices' do+ let(:new_product) { duplicate.value }++ before do+ variant1.prices.destroy_all+ variant1.prices = [+ create(:price, amount: 14.99, compare_at_amount: 15.99, currency: 'USD'),+ create(:price, amount: 12.99, compare_at_amount: 13.99, currency: 'GBP')+ ]++ variant2.prices.destroy_all+ variant2.prices = [+ create(:price, amount: 15.99, compare_at_amount: 16.99, currency: 'USD'),+ create(:price, amount: 13.99, compare_at_amount: 14.99, currency: 'GBP')+ ]+ end++ it 'clones prices' do+ expect(duplicate).to be_success++ variant1_usd_price = variant1.price_in('USD')+ expect(variant1_usd_price.amount).to eq(14.99)+ expect(variant1_usd_price.compare_at_amount).to eq(15.99)++ variant1_gbp_price = variant1.price_in('GBP')+ expect(variant1_gbp_price.amount).to eq(12.99)+ expect(variant1_gbp_price.compare_at_amount).to eq(13.99)++ variant2_usd_price = variant2.price_in('USD')+ expect(variant2_usd_price.amount).to eq(15.99)+ expect(variant2_usd_price.compare_at_amount).to eq(16.99)++ variant2_gbp_price = variant2.price_in('GBP')+ expect(variant2_gbp_price.amount).to eq(13.99)+ expect(variant2_gbp_price.compare_at_amount).to eq(14.99)+ end+ endendend