diff --git a/core/app/models/spree/adjustment.rb b/core/app/models/spree/adjustment.rb index 3d9362c022b..9b19bfa063c 100644 --- a/core/app/models/spree/adjustment.rb +++ b/core/app/models/spree/adjustment.rb @@ -55,11 +55,12 @@ class Adjustment < Spree::Base # # @param excluded_orders [Array] Orders to exclude from query # @return [ActiveRecord::Relation] Scoped Adjustments - def self.in_completed_orders(excluded_orders: []) - joins(:order). - merge(Spree::Order.complete). - where.not(spree_orders: { id: excluded_orders }). - distinct + def self.in_completed_orders(excluded_orders: [], exclude_canceled: false) + result = joins(:order) + .merge(Spree::Order.complete) + .where.not(spree_orders: { id: excluded_orders }) + .distinct + exclude_canceled ? result.merge(Spree::Order.not_canceled) : result end def finalize! diff --git a/core/app/models/spree/promotion.rb b/core/app/models/spree/promotion.rb index aa76deabf43..c39a1f6fe01 100644 --- a/core/app/models/spree/promotion.rb +++ b/core/app/models/spree/promotion.rb @@ -192,7 +192,7 @@ def usage_limit_exceeded?(excluded_orders: []) def usage_count(excluded_orders: []) Spree::Adjustment.promotion. eligible. - in_completed_orders(excluded_orders: excluded_orders). + in_completed_orders(excluded_orders: excluded_orders, exclude_canceled: true). where(source_id: actions). count(:order_id) end diff --git a/core/app/models/spree/promotion_code.rb b/core/app/models/spree/promotion_code.rb index c207fc62977..fbd37349b1b 100644 --- a/core/app/models/spree/promotion_code.rb +++ b/core/app/models/spree/promotion_code.rb @@ -30,7 +30,7 @@ def usage_limit_exceeded?(excluded_orders: []) def usage_count(excluded_orders: []) adjustments. eligible. - in_completed_orders(excluded_orders: excluded_orders). + in_completed_orders(excluded_orders: excluded_orders, exclude_canceled: true). count(:order_id) end diff --git a/core/spec/models/spree/promotion_code_spec.rb b/core/spec/models/spree/promotion_code_spec.rb index e048dfdcdd5..c838007ac5f 100644 --- a/core/spec/models/spree/promotion_code_spec.rb +++ b/core/spec/models/spree/promotion_code_spec.rb @@ -147,6 +147,11 @@ before { order.adjustments.promotion.update_all(eligible: false) } it { is_expected.to eq 0 } end + context "and the order is canceled" do + before { order.cancel! } + it { is_expected.to eq 0 } + it { expect(order.state).to eq 'canceled' } + end end end diff --git a/core/spec/models/spree/promotion_spec.rb b/core/spec/models/spree/promotion_spec.rb index 09ae1b699a8..01d3ab5c98b 100644 --- a/core/spec/models/spree/promotion_spec.rb +++ b/core/spec/models/spree/promotion_spec.rb @@ -386,6 +386,11 @@ before { order.adjustments.promotion.update_all(eligible: false) } it { is_expected.to eq 0 } end + context "and the order is canceled" do + before { order.cancel! } + it { is_expected.to eq 0 } + it { expect(order.state).to eq 'canceled' } + end end end