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
Fixed `Promotion#adjusted_credits_count` with Carts and Orders split
Damian Legawiece8a1a2504efc
diff --git a/spree/core/app/models/spree/promotion.rb b/spree/core/app/models/spree/promotion.rbindex 42ffa53ab7..3c1de02ca9 100644--- a/spree/core/app/models/spree/promotion.rb+++ b/spree/core/app/models/spree/promotion.rb@@ -248,7 +248,12 @@ module Spreeenddef adjusted_credits_count(promotable)- own_credits = if promotable.is_a?(Order)+ own_credits = case promotable+ when Spree::Cart+ # Cart-side discount rows carry no order_id and are+ # never part of credits_count — nothing to subtract.+ 0+ when Ordercredits.where(order_id: promotable.id).select(:order_id).distinct.countelsecredits.where(line_item_id: promotable.id).select(:order_id).distinct.countdiff --git a/spree/core/spec/models/spree/promotion_spec.rb b/spree/core/spec/models/spree/promotion_spec.rbindex ac60adffe6..7c42535335 100644--- a/spree/core/spec/models/spree/promotion_spec.rb+++ b/spree/core/spec/models/spree/promotion_spec.rb@@ -328,6 +328,7 @@ describe Spree::Promotion, type: :model doendend+context '#expired' doit 'is not exipired' doexpect(promotion).not_to be_expired@@ -444,7 +445,7 @@ describe Spree::Promotion, type: :model doendend- context '#adjusted_credits_count' do+ describe '#adjusted_credits_count' dolet(:order) { create :order }let(:line_item) { create :line_item, order: order }let(:promotion) { create(:promotion, name: 'promo', code: '10off') }@@ -490,6 +491,40 @@ describe Spree::Promotion, type: :model doexpect(promotion.credits_count).to eq(1)expect(promotion.adjusted_credits_count(order)).to eq(0)end++ context 'across carts and orders' do+ let(:promotion) { create(:promotion_with_item_adjustment, store: @default_store, usage_limit: 1) }+ let(:action) { promotion.promotion_actions.first }+ let!(:consuming_order) { create(:order_with_line_items, store: @default_store, line_items_count: 1) }++ before do+ create(:discount, order: consuming_order, line_item: consuming_order.line_items.first,+ promotion: promotion, promotion_action: action, kind: 'promotion', amount: -1)+ end++ it 'counts only order-side credits — carts never consume usage' do+ cart = create(:cart_with_line_items, store: @default_store)+ create(:discount, order: nil, cart: cart, line_item: cart.line_items.first,+ promotion: promotion, promotion_action: action, kind: 'promotion', amount: -1)++ expect(promotion.credits_count).to eq(1)+ expect(promotion.usage_limit_exceeded?(cart)).to be true+ end++ it 'does not subtract another order credit on a cart/line-item id collision' do+ # A cart whose id collides with the consuming order's line item id used+ # to fall into the LineItem branch and subtract that order's credit,+ # letting the cart bypass the usage limit.+ cart = create(:cart_with_line_items, store: @default_store)+ cart.update_columns(id: consuming_order.line_items.first.id) if Spree::Cart.where(id: consuming_order.line_items.first.id).none?++ expect(promotion.adjusted_credits_count(Spree::Cart.find(cart.reload.id))).to eq(1)+ end++ it 'subtracts the order own credit when checking that order' do+ expect(promotion.adjusted_credits_count(consuming_order)).to eq(0)+ end+ endendcontext '#products' do