diff --git a/core/app/models/spree/adjustment.rb b/core/app/models/spree/adjustment.rb index 3d9362c022b..418bad932ad 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_cancelled: false) + result = joins(:order) + .merge(Spree::Order.complete) + .where.not(spree_orders: { id: excluded_orders }) + .distinct + exclude_cancelled ? 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 77408609fc5..3945905944c 100644 --- a/core/app/models/spree/promotion.rb +++ b/core/app/models/spree/promotion.rb @@ -192,8 +192,7 @@ def usage_limit_exceeded?(excluded_orders: []) def usage_count(excluded_orders: []) Spree::Adjustment.promotion. eligible. - in_completed_orders(excluded_orders: excluded_orders). - where(spree_orders: { id: Spree::Order.not_canceled }). + in_completed_orders(excluded_orders: excluded_orders, exclude_cancelled: 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 124d0599909..ec7b87f3995 100644 --- a/core/app/models/spree/promotion_code.rb +++ b/core/app/models/spree/promotion_code.rb @@ -30,8 +30,7 @@ def usage_limit_exceeded?(excluded_orders: []) def usage_count(excluded_orders: []) adjustments. eligible. - in_completed_orders(excluded_orders: excluded_orders). - where(spree_orders: { id: Spree::Order.not_canceled }). + in_completed_orders(excluded_orders: excluded_orders, exclude_cancelled: true). count(:order_id) end