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.

Fixed: properly compare User/Zone IDs in User/Zone Rule casting them to strings

Damian Legawiec63bf69fddefd
diff --git a/core/app/models/spree/price_rules/user_rule.rb b/core/app/models/spree/price_rules/user_rule.rb
index 59185a9fcb..5b8350cfdd 100644
--- a/core/app/models/spree/price_rules/user_rule.rb
+++ b/core/app/models/spree/price_rules/user_rule.rb
@@ -7,7 +7,8 @@ module Spree
return false unless context.user
return true if preferred_user_ids.empty?
- preferred_user_ids.include?(context.user.id)
+ # Compare as strings to support both integer and UUID primary keys
+ preferred_user_ids.map(&:to_s).include?(context.user.id.to_s)
end
def self.description
diff --git a/core/app/models/spree/price_rules/zone_rule.rb b/core/app/models/spree/price_rules/zone_rule.rb
index 7004722445..9d9b40230a 100644
--- a/core/app/models/spree/price_rules/zone_rule.rb
+++ b/core/app/models/spree/price_rules/zone_rule.rb
@@ -7,7 +7,8 @@ module Spree
return false unless context.zone
return true if preferred_zone_ids.empty?
- preferred_zone_ids.include?(context.zone.id)
+ # Compare as strings to support both integer and UUID primary keys
+ preferred_zone_ids.map(&:to_s).include?(context.zone.id.to_s)
end
def self.description
diff --git a/core/spec/models/spree/price_rules/user_rule_spec.rb b/core/spec/models/spree/price_rules/user_rule_spec.rb
index 86f6943de6..eb5430f29a 100644
--- a/core/spec/models/spree/price_rules/user_rule_spec.rb
+++ b/core/spec/models/spree/price_rules/user_rule_spec.rb
@@ -35,5 +35,20 @@ describe Spree::PriceRules::UserRule, type: :model do
expect(rule.applicable?(context)).to be false
end
end
+
+ context 'when user_ids preference contains strings' do
+ before { rule.preferred_user_ids = [user.id.to_s] }
+
+ it 'returns true when context user matches' do
+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', user: user)
+ expect(rule.applicable?(context)).to be true
+ end
+
+ it 'returns false when context user does not match' do
+ other_user = create(:user)
+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', user: other_user)
+ expect(rule.applicable?(context)).to be false
+ end
+ end
end
end
diff --git a/core/spec/models/spree/price_rules/zone_rule_spec.rb b/core/spec/models/spree/price_rules/zone_rule_spec.rb
index 6637a68541..93eb36ec58 100644
--- a/core/spec/models/spree/price_rules/zone_rule_spec.rb
+++ b/core/spec/models/spree/price_rules/zone_rule_spec.rb
@@ -35,5 +35,20 @@ describe Spree::PriceRules::ZoneRule, type: :model do
expect(rule.applicable?(context)).to be false
end
end
+
+ context 'when zone_ids preference contains strings' do
+ before { rule.preferred_zone_ids = [zone.id.to_s] }
+
+ it 'returns true when context zone matches' do
+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', zone: zone)
+ expect(rule.applicable?(context)).to be true
+ end
+
+ it 'returns false when context zone does not match' do
+ other_zone = create(:zone)
+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', zone: other_zone)
+ expect(rule.applicable?(context)).to be false
+ end
+ end
end
end