Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Count a promotion's use by the checkout, not by the order

Damian Legawiec53d9d994a408
diff --git a/spree/core/app/models/spree/order.rb b/spree/core/app/models/spree/order.rb
index de8167d886..a159f60b46 100644
--- a/spree/core/app/models/spree/order.rb
+++ b/spree/core/app/models/spree/order.rb
@@ -591,6 +591,18 @@ module Spree
order_group_id.present?
end
+ # This order and any placed alongside it in the same checkout.
+ #
+ # The set anything counting *checkouts* rather than orders works from — a
+ # promotion's usage limit, for one, which must not be spent several times
+ # over because a basket happened to span several sellers.
+ #
+ # @return [Array<Integer>]
+ def sibling_order_ids
+ return [id] unless grouped?
+
+ order_group.orders.ids
+ end
# This order's share of one payment made against its group.
#
diff --git a/spree/core/app/models/spree/promotion.rb b/spree/core/app/models/spree/promotion.rb
index 94671ea5aa..fb00c49408 100644
--- a/spree/core/app/models/spree/promotion.rb
+++ b/spree/core/app/models/spree/promotion.rb
@@ -254,9 +254,12 @@ module Spree
# never part of credits_count — nothing to subtract.
0
when Order
- credits.where(order_id: promotable.id).select(:order_id).distinct.count
+ # A checkout that divided across sellers left a discount
+ # row on every child, so what this promotable already
+ # accounts for is its whole group, not just itself.
+ checkouts_credited(credits.where(order_id: promotable.sibling_order_ids))
else
- credits.where(line_item_id: promotable.id).select(:order_id).distinct.count
+ checkouts_credited(credits.where(line_item_id: promotable.id))
end
credits_count - own_credits
end
@@ -265,8 +268,14 @@ module Spree
Spree::Discount.promotion.where(promotion_action_id: actions.map(&:id))
end
+ # How many times this promotion has been redeemed.
+ #
+ # Counted in checkouts rather than orders: a basket spanning several
+ # sellers becomes several orders, each carrying its own discount rows, and
+ # counting those as separate uses would spend a limited promotion two or
+ # three times over on one customer's single redemption.
def credits_count
- credits.select(:order_id).distinct.count
+ checkouts_credited(credits)
end
def line_item_actionable?(order, line_item)
@@ -284,9 +293,17 @@ module Spree
end
end
+ # @param excluded_orders [Array<Spree::Order>] orders whose own use does
+ # not count — an order asking on its own behalf. Excluding one of a split
+ # checkout's orders excludes its siblings too, since they are one
+ # redemption between them rather than several.
def used_by?(user, excluded_orders = [])
+ # A cart is not an order and has no siblings; it simply has nothing to
+ # exclude, since its own discounts carry no order_id.
+ excluded_ids = excluded_orders.flat_map { |record| record.try(:sibling_order_ids) || [] }.uniq
+
user.orders.complete.joins(:discounts).
- where.not(spree_orders: { id: excluded_orders.map(&:id) }).
+ where.not(spree_orders: { id: excluded_ids }).
where(Spree::Discount.table_name => { promotion_id: id, kind: 'promotion' }).any?
end
@@ -308,6 +325,22 @@ module Spree
private
+ # Counts distinct checkouts behind a set of discount rows.
+ #
+ # An order placed on its own is one checkout; the several orders a split
+ # checkout produced are one between them, which is what their shared group
+ # identifies. Collapsing on the group is what makes a redemption cost the
+ # same whether the customer bought from one seller or four.
+ #
+ # @param scope [ActiveRecord::Relation<Spree::Discount>]
+ # @return [Integer]
+ def checkouts_credited(scope)
+ orders = Spree::Order.arel_table
+ checkout = Arel::Nodes::NamedFunction.new('COALESCE', [orders[:order_group_id], orders[:id]])
+
+ scope.joins(:order).distinct.count(checkout)
+ end
+
def apply_pending_rules_and_actions
flush_pending_typed_association(:promotion_rules)
flush_pending_typed_association(:promotion_actions)
diff --git a/spree/core/spec/models/spree/promotion_usage_across_split_spec.rb b/spree/core/spec/models/spree/promotion_usage_across_split_spec.rb
new file mode 100644
index 0000000000..360adfd599
--- /dev/null
+++ b/spree/core/spec/models/spree/promotion_usage_across_split_spec.rb
@@ -0,0 +1,89 @@
+require 'spec_helper'
+
+# A promotion is redeemed by a customer, once, at a checkout. That a basket
+# happened to span several sellers — and so became several orders — must not
+# spend a limited promotion two or three times over.
+RSpec.describe Spree::Promotion, 'usage counted across a split checkout' do
+ let(:store) { @default_store }
+ let(:promotion) { create(:promotion, :with_order_adjustment, store: store, usage_limit: 2) }
+ let(:action) { promotion.actions.first }
+
+ # One checkout that produced `orders_count` orders, each carrying its own
+ # discount row — which is what the split leaves behind.
+ def redeem(orders_count)
+ group = orders_count > 1 ? create(:order_group, store: store) : nil
+
+ Array.new(orders_count) do
+ order = create(:order, store: store, order_group: group, total: 10)
+ line_item = create(:line_item, order: order)
+ Spree::Discount.create!(
+ order: order, line_item: line_item, promotion: promotion, promotion_action: action,
+ kind: 'promotion', amount: -1, label: promotion.name
+ )
+ order
+ end
+ end
+
+ describe '#credits_count' do
+ it 'counts one checkout once, however many sellers it reached' do
+ redeem(3)
+
+ expect(promotion.credits_count).to eq(1)
+ end
+
+ it 'counts separate checkouts separately' do
+ redeem(2)
+ redeem(1)
+
+ expect(promotion.credits_count).to eq(2)
+ end
+ end
+
+ describe '#usage_limit_exceeded?' do
+ it 'does not exhaust a two-use promotion on one split checkout' do
+ redeem(3)
+ next_order = create(:order, store: store, total: 10)
+
+ expect(promotion.usage_limit_exceeded?(next_order)).to be(false)
+ end
+
+ it 'still stops at the limit across distinct checkouts' do
+ redeem(2)
+ redeem(1)
+ next_order = create(:order, store: store, total: 10)
+
+ expect(promotion.usage_limit_exceeded?(next_order)).to be(true)
+ end
+
+ # An order asking whether it may still use a promotion must not be told no
+ # on the strength of its own siblings' rows.
+ it 'discounts the asking order’s whole group from the tally' do
+ orders = redeem(2)
+
+ expect(promotion.adjusted_credits_count(orders.first)).to eq(0)
+ end
+ end
+
+ describe '#used_by?' do
+ let(:customer) { create(:user) }
+
+ def redeem_for(customer, orders_count)
+ redeem(orders_count).each do |order|
+ order.update_columns(customer_id: customer.id, status: 'placed', completed_at: Time.current)
+ end
+ end
+
+ it 'does not count a customer’s own split checkout against them' do
+ orders = redeem_for(customer, 3)
+
+ expect(promotion.used_by?(customer, [orders.first])).to be(false)
+ end
+
+ it 'still reports a promotion used on an earlier checkout' do
+ redeem_for(customer, 2)
+ later = redeem(1).first
+
+ expect(promotion.used_by?(customer, [later])).to be(true)
+ end
+ end
+end