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
Fix promotion usage count double-counting with multiple actions (#13889)
Kenta Ishizakic1ab503e57f0
diff --git a/spree/core/app/models/spree/promotion.rb b/spree/core/app/models/spree/promotion.rbindex d05d37e990..230bf52e19 100644--- a/spree/core/app/models/spree/promotion.rb+++ b/spree/core/app/models/spree/promotion.rb@@ -232,7 +232,7 @@ module Spreedef adjusted_credits_count(promotable)adjustments = promotable.is_a?(Order) ? promotable.all_adjustments : promotable.adjustments- credits_count - adjustments.promotion.where(source_id: actions.pluck(:id)).size+ credits_count - adjustments.eligible.promotion.where(source_id: actions.pluck(:id)).select(:order_id).distinct.countenddef credits@@ -240,7 +240,7 @@ module Spreeenddef credits_count- credits.count+ credits.select(:order_id).distinct.countenddef line_item_actionable?(order, line_item)diff --git a/spree/core/spec/models/spree/promotion/actions/free_shipping_spec.rb b/spree/core/spec/models/spree/promotion/actions/free_shipping_spec.rbindex c7ec7ec82f..e18b1cc2cf 100644--- a/spree/core/spec/models/spree/promotion/actions/free_shipping_spec.rb+++ b/spree/core/spec/models/spree/promotion/actions/free_shipping_spec.rb@@ -23,7 +23,7 @@ describe Spree::Promotion::Actions::FreeShipping, type: :model doexpect(first_shipment_cost).to be > 0expect(last_shipment_cost).to be > 0expect(action.perform(payload)).to be true- expect(promotion.credits_count).to eq(2)+ expect(promotion.credits_count).to eq(1)expect(order.shipment_adjustments.count).to eq(2)expect(order.shipment_adjustments.first.amount.to_i).to eq(-first_shipment_cost.to_i)expect(order.shipment_adjustments.last.amount.to_i).to eq(-last_shipment_cost.to_i)@@ -32,7 +32,7 @@ describe Spree::Promotion::Actions::FreeShipping, type: :model doit 'does not create a discount when order already has one from this promotion' doexpect(action.perform(payload)).to be trueexpect(action.perform(payload)).to be false- expect(promotion.credits_count).to eq(2)+ expect(promotion.credits_count).to eq(1)expect(order.shipment_adjustments.count).to eq(2)end@@ -46,7 +46,7 @@ describe Spree::Promotion::Actions::FreeShipping, type: :model doexpect(order.shipments.first.cost).to eq(0)expect(order.shipments.last.cost).to eq(0)expect(action.perform(payload)).to be true- expect(promotion.credits_count).to eq(2)+ expect(promotion.credits_count).to eq(1)expect(order.shipment_adjustments.count).to eq(2)expect(order.shipment_adjustments.first.amount.to_i).to eq(0)expect(order.shipment_adjustments.last.amount.to_i).to eq(0)diff --git a/spree/core/spec/models/spree/promotion_spec.rb b/spree/core/spec/models/spree/promotion_spec.rbindex 6ba8ed89f4..36d0c53a5e 100644--- a/spree/core/spec/models/spree/promotion_spec.rb+++ b/spree/core/spec/models/spree/promotion_spec.rb@@ -391,6 +391,54 @@ describe Spree::Promotion, type: :model doadjustment.update_column(:eligible, false)expect(promotion.credits_count).to eq(0)end++ # Regression test for #11879+ context 'with multiple actions on the same order' do+ let!(:second_action) do+ calculator = Spree::Calculator::FlatRate.new+ action = Spree::Promotion::Actions::CreateAdjustment.create(promotion: promotion, calculator: calculator)+ promotion.actions << action+ action+ end++ before do+ order = adjustment.order+ Spree::Adjustment.create!(+ order: order,+ adjustable: order,+ source: second_action,+ amount: -5,+ label: 'Second promotional adjustment'+ )+ # mark both adjustments eligible+ order.all_adjustments.update_all(eligible: true)+ end++ it 'counts unique orders, not individual adjustments' do+ expect(promotion.credits_count).to eq(1)+ end+ end++ # Regression test for #11879+ context 'with the same action applied to multiple orders' do+ let!(:second_order) { create(:order) }++ before do+ Spree::Adjustment.create!(+ order: second_order,+ adjustable: second_order,+ source: action,+ amount: 10,+ label: 'Promotional adjustment'+ )+ adjustment.update_column(:eligible, true)+ second_order.all_adjustments.update_all(eligible: true)+ end++ it 'counts each order separately' do+ expect(promotion.credits_count).to eq(2)+ end+ endendcontext '#adjusted_credits_count' do