diff --git a/core/app/models/spree/promotion.rb b/core/app/models/spree/promotion.rb index aa76deabf43..77408609fc5 100644 --- a/core/app/models/spree/promotion.rb +++ b/core/app/models/spree/promotion.rb @@ -193,6 +193,7 @@ def usage_count(excluded_orders: []) Spree::Adjustment.promotion. eligible. in_completed_orders(excluded_orders: excluded_orders). + where(spree_orders: { id: Spree::Order.not_canceled }). 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..124d0599909 100644 --- a/core/app/models/spree/promotion_code.rb +++ b/core/app/models/spree/promotion_code.rb @@ -31,6 +31,7 @@ def usage_count(excluded_orders: []) adjustments. eligible. in_completed_orders(excluded_orders: excluded_orders). + where(spree_orders: { id: Spree::Order.not_canceled }). 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