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.

Make ItemTotal rule max parameter nullable

Damian Legawiecf0bfa82a4194
diff --git a/core/app/models/spree/promotion/rules/item_total.rb b/core/app/models/spree/promotion/rules/item_total.rb
index b72196109b..55d3b55496 100644
--- a/core/app/models/spree/promotion/rules/item_total.rb
+++ b/core/app/models/spree/promotion/rules/item_total.rb
@@ -6,7 +6,7 @@ module Spree
class ItemTotal < PromotionRule
preference :amount_min, :decimal, default: 100.00
preference :operator_min, :string, default: '>'
- preference :amount_max, :decimal, default: 1000.00
+ preference :amount_max, :decimal, default: nil, nullable: true
preference :operator_max, :string, default: '<'
OPERATORS_MIN = ['gt', 'gte']
@@ -20,7 +20,12 @@ module Spree
item_total = order.item_total
lower_limit_condition = item_total.send(preferred_operator_min == 'gte' ? :>= : :>, BigDecimal(preferred_amount_min.to_s))
- upper_limit_condition = item_total.send(preferred_operator_max == 'lte' ? :<= : :<, BigDecimal(preferred_amount_max.to_s))
+
+ if preferred_amount_max.present?
+ upper_limit_condition = item_total.send(preferred_operator_max == 'lte' ? :<= : :<, BigDecimal(preferred_amount_max.to_s))
+ else
+ upper_limit_condition = true
+ end
eligibility_errors.add(:base, ineligible_message_max) unless upper_limit_condition
eligibility_errors.add(:base, ineligible_message_min) unless lower_limit_condition
@@ -35,7 +40,11 @@ module Spree
end
def formatted_amount_max
- Spree::Money.new(preferred_amount_max).to_s
+ if preferred_amount_max.present?
+ Spree::Money.new(preferred_amount_max).to_s
+ else
+ Spree.t('no_maximum')
+ end
end
def ineligible_message_max
diff --git a/core/spec/models/spree/promotion/rules/item_total_spec.rb b/core/spec/models/spree/promotion/rules/item_total_spec.rb
index e0769fcc5b..3e837eee62 100644
--- a/core/spec/models/spree/promotion/rules/item_total_spec.rb
+++ b/core/spec/models/spree/promotion/rules/item_total_spec.rb
@@ -273,4 +273,48 @@ describe Spree::Promotion::Rules::ItemTotal, type: :model do
end
end
end
+
+ context 'when preferred amount max is not set' do
+ before do
+ rule.preferred_amount_min = 50
+ rule.preferred_amount_max = nil
+ rule.preferred_operator_min = 'gt'
+ rule.preferred_operator_max = 'lt'
+ end
+
+ context 'and item total is higher than preferred minimum amount' do
+ it 'is eligible' do
+ allow(order).to receive_messages item_total: 51
+ expect(rule).to be_eligible(order)
+ end
+ end
+
+ context 'and item total is equal to the preferred minimum amount' do
+ before { allow(order).to receive_messages item_total: 50 }
+
+ it 'is not eligible' do
+ expect(rule).not_to be_eligible(order)
+ end
+
+ it 'set an error message' do
+ rule.eligible?(order)
+ expect(rule.eligibility_errors.full_messages.first).
+ to eq "This coupon code can't be applied to orders less than or equal to $50.00."
+ end
+ end
+
+ context 'and item total is lower than preferred minimum amount' do
+ before { allow(order).to receive_messages item_total: 49 }
+
+ it 'is not eligible' do
+ expect(rule).not_to be_eligible(order)
+ end
+
+ it 'set an error message' do
+ rule.eligible?(order)
+ expect(rule.eligibility_errors.full_messages.first).
+ to eq "This coupon code can't be applied to orders less than or equal to $50.00."
+ end
+ end
+ end
end