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
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.rbindex 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 Spreeclass ItemTotal < PromotionRulepreference :amount_min, :decimal, default: 100.00preference :operator_min, :string, default: '>'- preference :amount_max, :decimal, default: 1000.00+ preference :amount_max, :decimal, default: nil, nullable: truepreference :operator_max, :string, default: '<'OPERATORS_MIN = ['gt', 'gte']@@ -20,7 +20,12 @@ module Spreeitem_total = order.item_totallower_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+ endeligibility_errors.add(:base, ineligible_message_max) unless upper_limit_conditioneligibility_errors.add(:base, ineligible_message_min) unless lower_limit_condition@@ -35,7 +40,11 @@ module Spreeenddef 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')+ endenddef ineligible_message_maxdiff --git a/core/spec/models/spree/promotion/rules/item_total_spec.rb b/core/spec/models/spree/promotion/rules/item_total_spec.rbindex 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 doendendend++ 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+ endend