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
Address review feedback on the default variant cutover
Łukasz Adamczykb0e4c004e098
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rbindex c8d119734b..5e4fa7747f 100644--- a/spree/core/app/models/spree/product.rb+++ b/spree/core/app/models/spree/product.rb@@ -136,6 +136,10 @@ module Spreevalidates :price, if: :requires_price?end+ # Every persisted product must keep a default variant. It's assigned on+ # create by +set_default_variant+, so only enforce it on update.+ validates :default_variant, presence: true, on: :update+validate :discontinue_on_must_be_later_than_make_active_at, if: -> { make_active_at && discontinue_on }scope :for_store, ->(store) { where(store_id: store.id) }@@ -256,18 +260,20 @@ module Spreeascend_by_price descend_by_price]# All product-level convenience attributes delegate to the default variant —- # one target, no branching. Setters build the default variant on demand so- # they work before the product is persisted (e.g. `product.sku = ...`).+ # one target, no branching. Reads resolve the existing default variant+ # (nil-safe) and never build one as a side effect; only setters build on+ # demand, so `product.sku = ...` still works before the product is persisted.[:sku, :barcode, :weight, :height, :width, :depth, :dimensions_unit, :weight_unit,:price, :price_in, :amount_in, :compare_at_price, :compare_at_amount_in,:currency, :cost_currency, :cost_price, :track_inventory].each do |method_name|- delegate method_name, :"#{method_name}=", to: :find_or_build_default_variant+ delegate method_name, to: :default_variant, allow_nil: true+ delegate :"#{method_name}=", to: :find_or_build_default_variantenddelegate :display_amount, :display_price, :has_default_price?, :track_inventory?,- :display_compare_at_price, :images, to: :find_or_build_default_variant+ :display_compare_at_price, :images, to: :default_variant, allow_nil: truestate_machine :status, initial: :draft doevent :activate do@@ -740,14 +746,21 @@ module Spreevariant_data = variant_data.to_h.with_indifferent_accessvariant_id = variant_data.delete(:id)- if variant_id.present?- variant = variants.find_by_param!(variant_id)- variant.update!(variant_data)- else- variant = variants.build- variant.assign_attributes(variant_data)- variant.save!- end+ variant =+ if variant_id.present?+ variants.find_by_param!(variant_id)+ elsif reuse_default_variant_for?(variant_data, variant_ids_in_payload)+ # An id-less, option-less entry targets the product's existing+ # option-less default variant (the simple-product case) — update it in+ # place rather than building a new row, so its id, order-line+ # references, and unspecified prices/stock survive.+ default_variant+ else+ variants.build+ end++ variant.assign_attributes(variant_data)+ variant.save!variant_ids_in_payload << variant.idmutated = true@@ -762,6 +775,18 @@ module Spreesync_variant_state! if mutatedend+ # True when an id-less variant payload should update the product's existing+ # option-less default variant instead of building a new one: the entry carries+ # no options, a default variant exists and is itself option-less, and it hasn't+ # already been claimed by an earlier entry in this payload.+ def reuse_default_variant_for?(variant_data, consumed_ids)+ variant_data[:options].blank? &&+ variant_data[:option_value_variants_attributes].blank? &&+ default_variant.present? &&+ default_variant.option_values.empty? &&+ consumed_ids.exclude?(default_variant.id)+ end+# Re-syncs the in-memory derived variant state after `apply_variants`# mutates variants out-of-band. `variant_count` is bumped via# `Spree::Product.increment_counter` in a Variant callback, so the in-memorydiff --git a/spree/core/app/services/spree/products/duplicator.rb b/spree/core/app/services/spree/products/duplicator.rbindex 84328368b0..9a9d610c44 100644--- a/spree/core/app/services/spree/products/duplicator.rb+++ b/spree/core/app/services/spree/products/duplicator.rb@@ -6,8 +6,10 @@ module Spreedef call(product:, include_images: true)new_product = duplicate_product(product, include_images)- # don't dup the actual variants, just the characterising types- new_product.option_types = product.option_types if product.has_variants?+ # don't dup the actual variants, just the characterising types. Gate on+ # option_types (not has_variants?, which is >1) so a single option-bearing+ # variant still carries its option types across.+ new_product.option_types = product.option_types if product.option_types.any?# allow site to do some customizationnew_product.send(:duplicate_extra, product) if new_product.respond_to?(:duplicate_extra)diff --git a/spree/core/lib/spree/core/configuration.rb b/spree/core/lib/spree/core/configuration.rbindex 47565ca648..4bfb556599 100644--- a/spree/core/lib/spree/core/configuration.rb+++ b/spree/core/lib/spree/core/configuration.rb@@ -90,6 +90,7 @@ module Spreepreference :non_expiring_credit_types, :array, default: []preference :products_per_page, :integer, default: 12preference :require_default_variant_price, :boolean, default: false+ preference :require_master_price, :boolean, default: false, deprecated: 'Use require_default_variant_price instead' # renamed in 6.0 with the master variant removal; removed in 6.1preference :restock_inventory, :boolean, default: true # Determines if a return item is restocked automatically once it has been receivedpreference :return_eligibility_number_of_days, :integer, default: 365preference :reserve_stock_on, :string, default: 'checkout' # 'checkout' (default) or 'cart' — landing in 6.0 alongside Cart/Order splitdiff --git a/spree/core/spec/models/spree/product_spec.rb b/spree/core/spec/models/spree/product_spec.rbindex e80512c303..7cdf879d7e 100644--- a/spree/core/spec/models/spree/product_spec.rb+++ b/spree/core/spec/models/spree/product_spec.rb@@ -501,6 +501,35 @@ describe Spree::Product, type: :model doendend+ describe '#variants= on a persisted product' do+ let!(:product) { create(:product) }+ let!(:default_variant) { product.default_variant }++ context 'with an id-less, option-less entry (simple-product update)' do+ it 'updates the existing default variant in place instead of replacing it' do+ product.variants = [{ sku: 'UPDATED-SKU' }]+ product.reload++ # The original variant is upserted — same row, not replaced — so its id+ # (and any order-line references to it) survive.+ expect(product.default_variant.id).to eq(default_variant.id)+ expect(product.default_variant.sku).to eq('UPDATED-SKU')+ expect(product.variants.count).to eq(1)+ end+ end++ context 'with an option-bearing entry' do+ it 'builds a new variant and drops the option-less placeholder' do+ product.variants = [{ sku: 'TEE-S', options: [{ name: 'size', value: 'Small' }] }]+ product.reload++ expect(product.variants.pluck(:sku)).to contain_exactly('TEE-S')+ expect(product.default_variant.option_values.map(&:name)).to include('small')+ expect(Spree::Variant.find_by(id: default_variant.id)).to be_nil+ end+ end+ end+describe '#images' dolet(:product) { create(:product) }let(:file) { File.open(File.expand_path('../../fixtures/thinking-cat.jpg', __dir__)) }