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.

Remov `apply_to` from VolumeRule

Damian Legawiecc95ec9e0ea61
diff --git a/core/app/models/spree/price_rules/volume_rule.rb b/core/app/models/spree/price_rules/volume_rule.rb
index ba66509d9a..d0b49a7007 100644
--- a/core/app/models/spree/price_rules/volume_rule.rb
+++ b/core/app/models/spree/price_rules/volume_rule.rb
@@ -3,18 +3,12 @@ module Spree
class VolumeRule < Spree::PriceRule
preference :min_quantity, :integer, default: 1
preference :max_quantity, :integer
- preference :apply_to, :string, default: 'line_item'
-
- APPLY_TO_OPTIONS = %w[line_item cart_total].freeze
-
- validates :preferred_apply_to, inclusion: { in: APPLY_TO_OPTIONS }, allow_nil: true
def applicable?(context)
- quantity = calculate_quantity(context)
- return false unless quantity
+ return false unless context.quantity
- return false if quantity < preferred_min_quantity
- return false if preferred_max_quantity && quantity > preferred_max_quantity
+ return false if context.quantity < preferred_min_quantity
+ return false if preferred_max_quantity && context.quantity > preferred_max_quantity
true
end
@@ -22,17 +16,6 @@ module Spree
def self.description
'Apply pricing based on quantity purchased'
end
-
- private
-
- def calculate_quantity(context)
- case preferred_apply_to
- when 'line_item'
- context.quantity
- when 'cart_total'
- context.order&.line_items&.find_by(variant: context.variant)&.quantity
- end
- end
end
end
end
diff --git a/core/spec/models/spree/price_rules/volume_rule_spec.rb b/core/spec/models/spree/price_rules/volume_rule_spec.rb
index 01cc6bd0b7..48b8191826 100644
--- a/core/spec/models/spree/price_rules/volume_rule_spec.rb
+++ b/core/spec/models/spree/price_rules/volume_rule_spec.rb
@@ -29,5 +29,10 @@ describe Spree::PriceRules::VolumeRule, type: :model do
expect(rule.applicable?(context)).to be false
end
end
+
+ it 'returns false when quantity is nil' do
+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', quantity: nil)
+ expect(rule.applicable?(context)).to be false
+ end
end
end