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: Scoped the product duplicator SKU lookup to the product's store (#14958)
Damian Legawiecaf5cdbef7d70
diff --git a/spree/core/app/services/spree/products/duplicator.rb b/spree/core/app/services/spree/products/duplicator.rbindex 3078de4284..db84387c75 100644--- a/spree/core/app/services/spree/products/duplicator.rb+++ b/spree/core/app/services/spree/products/duplicator.rb@@ -42,7 +42,7 @@ module Spreedef duplicate_variant(variant, include_images)new_variant = variant.dup- new_variant.sku = sku_generator(new_variant.sku)+ new_variant.sku = sku_generator(variant)new_variant.deleted_at = nilnew_variant.option_values = variant.option_values.map { |option_value| option_value }new_variant.prices = duplicate_prices(variant.prices)@@ -82,10 +82,18 @@ module Spreenew_imageend- def sku_generator(sku)- return '' if sku.blank?+ def sku_generator(variant)+ return '' if variant.sku.blank?- "COPY OF #{Variant.unscoped.where('sku like ?', "%#{sku}").order(:created_at).last.sku}"+ store_product_ids = Product.with_deleted.where(store_id: variant.store_id).select(:id)+ # Matched the way the SKU uniqueness check compares, without regard+ # to case, and within the same seller scope it reads, so the name taken+ # from the latest match is one this seller may use.+ sku_pattern = Variant.arel_table[:sku].matches("%#{Variant.sanitize_sql_like(variant.sku)}", '\\', false)+ latest_match = Variant.with_deleted.for_seller(variant.resolved_seller_id).+ where(product_id: store_product_ids).where(sku_pattern).order(:created_at, :id).last++ "COPY OF #{(latest_match || variant).sku}"enddef duplicate_error_message(new_product)diff --git a/spree/core/spec/services/spree/products/duplicator_spec.rb b/spree/core/spec/services/spree/products/duplicator_spec.rbindex 4d5cc14534..5485f87ee3 100644--- a/spree/core/spec/services/spree/products/duplicator_spec.rb+++ b/spree/core/spec/services/spree/products/duplicator_spec.rb@@ -39,6 +39,45 @@ RSpec.describe Spree::Products::Duplicator doTimecop.returnend+ context 'when a variant in another store has a matching sku' do+ before { create(:product, store: create(:store), sku: "OTHER-#{product.sku}") }++ it 'names the copy after a variant of its own store' do+ expect(duplicate.value.sku).to eq "COPY OF #{product.sku}"+ end+ end++ context 'when a variant of another seller in the store has a matching sku' do+ before { create(:product, seller: create(:seller, :approved), sku: "OTHER-#{product.sku}") }++ it 'names the copy after a variant of its own seller' do+ expect(duplicate.value.sku).to eq "COPY OF #{product.sku}"+ end+ end++ context 'when an earlier copy differs from the sku only in case' do+ before do+ product.default_variant.update!(sku: 'ABC')+ create(:product, sku: 'COPY OF abc')+ end++ it 'counts it as taken, as the SKU uniqueness check does' do+ expect(duplicate.value.sku).to eq 'COPY OF COPY OF abc'+ end+ end++ context 'when the sku contains a wildcard character' do+ before { product.default_variant.update!(sku: 'SKU_1') }++ it 'advances the copy name on every duplication' do+ first_copy = duplicate.value+ second_copy = described_class.call(product: product.reload).value++ expect(first_copy.sku).to eq 'COPY OF SKU_1'+ expect(second_copy.sku).to eq 'COPY OF COPY OF SKU_1'+ end+ end+describe 'stock level duplication' do# A promise belongs to the order that made it. Copied onto the duplicate,# it would open holding stock for somebody else's order.