Skip to content

armgit / spree

Public Imported from GitHub
0

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.

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.rb
index 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 Spree
acts_as_list scope: :product
validates :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)
+ end
end
end
diff --git a/spree/core/app/models/spree/variant.rb b/spree/core/app/models/spree/variant.rb
index 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.store
o.save!
end
diff --git a/spree/core/spec/models/spree/option_type_spec.rb b/spree/core/spec/models/spree/option_type_spec.rb
index 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 do
end
end
+ # 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
+ end
end
diff --git a/spree/core/spec/models/spree/variant_spec.rb b/spree/core/spec/models/spree/variant_spec.rb
index 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 do
expect(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' do
multi_variant.set_option_value('media_type', 'CD')