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: matched variant option names only against the product's own store
Damian Legawiec84953347ee0e
diff --git a/spree/core/app/models/spree/product_option_type.rb b/spree/core/app/models/spree/product_option_type.rbindex 19fdd9198b..e2c2598492 100644--- a/spree/core/app/models/spree/product_option_type.rb+++ b/spree/core/app/models/spree/product_option_type.rb@@ -7,5 +7,16 @@ module Spreeacts_as_list scope: :productvalidates :product_id, uniqueness: { scope: :option_type_id }, allow_nil: true+ validate :option_type_belongs_to_product_store++ private++ # Option types are store-owned, so a product may only use its own store's.+ def option_type_belongs_to_product_store+ return if product.nil? || option_type.nil?+ return if option_type.store_id == product.store_id++ errors.add(:option_type, :invalid)+ endendenddiff --git a/spree/core/app/models/spree/variant.rb b/spree/core/app/models/spree/variant.rbindex 6647bf8d90..ab2104e509 100644--- a/spree/core/app/models/spree/variant.rb+++ b/spree/core/app/models/spree/variant.rb@@ -605,8 +605,11 @@ module Spree# @param opt_type_position [Integer] the position of the option type# @return [void]def set_option_value(opt_name, opt_value, opt_type_position = nil)- option_type = Spree::OptionType.where(name: opt_name.parameterize).first_or_initialize do |o|+ # The product's own store: option types are store-owned, and matching one+ # by name elsewhere would attach and extend another store's type.+ option_type = Spree::OptionType.for_store(product.store).where(name: opt_name.parameterize).first_or_initialize do |o|o.name = o.label = opt_name+ o.store = product.storeo.save!enddiff --git a/spree/core/spec/models/spree/option_type_spec.rb b/spree/core/spec/models/spree/option_type_spec.rbindex 4288471c79..b7c07f8321 100644--- a/spree/core/spec/models/spree/option_type_spec.rb+++ b/spree/core/spec/models/spree/option_type_spec.rb@@ -244,4 +244,20 @@ describe Spree::OptionType, type: :model doendend+ # Option types are store-owned; a product must not pick up another store's.+ describe 'attaching to a product' do+ let(:product) { create(:product) }++ it "refuses another store's option type" do+ foreign = create(:option_type, store: create(:store))++ expect(Spree::ProductOptionType.new(product: product, option_type: foreign)).not_to be_valid+ end++ it "accepts the product's own store's option type" do+ own = create(:option_type, store: product.store)++ expect(Spree::ProductOptionType.new(product: product, option_type: own)).to be_valid+ end+ endenddiff --git a/spree/core/spec/models/spree/variant_spec.rb b/spree/core/spec/models/spree/variant_spec.rbindex 36d3b71bef..b49e931d82 100644--- a/spree/core/spec/models/spree/variant_spec.rb+++ b/spree/core/spec/models/spree/variant_spec.rb@@ -506,6 +506,17 @@ describe Spree::Variant, type: :model doexpect(multi_variant.option_value('media_type')).to eql 'CD'end+ it "never attaches another store's option type of the same name" do+ foreign = create(:option_type, name: 'media_type', store: create(:store))++ multi_variant.set_option_value('media_type', 'DVD')++ option_type = multi_variant.option_values.first.option_type+ expect(option_type).not_to eq(foreign)+ expect(option_type.store).to eq(multi_variant.product.store)+ expect(foreign.option_values).to be_empty+ end+it 'does not duplicate associated option values when set multiple times' domulti_variant.set_option_value('media_type', 'CD')