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.

Fix: a coupon code on a draft order or spent mid-apply is never taken over

Damian Legawiec54662fbb80c1
diff --git a/spree/core/app/models/spree/promotion_handler/coupon.rb b/spree/core/app/models/spree/promotion_handler/coupon.rb
index 33bc8e7725..0d0899eb50 100644
--- a/spree/core/app/models/spree/promotion_handler/coupon.rb
+++ b/spree/core/app/models/spree/promotion_handler/coupon.rb
@@ -239,18 +239,18 @@ module Spree
end
# A single-use code is spent only when an order is placed with it, so a
- # code sitting on another open cart or draft order is taken from it
- # rather than refused: whoever presents it now gets the discount, and
- # whoever places an order first keeps the code. A holder in the middle of
- # checkout keeps it, since its totals are fixed and money may already be
- # at the gateway.
+ # code sitting on another open cart is taken from it rather than
+ # refused: whoever presents it now gets the discount, and whoever places
+ # an order first keeps the code. A cart in the middle of checkout keeps
+ # it, and so does a draft order, since its totals are fixed and money
+ # may already be at the gateway.
#
# The holder is locked before the code, the same order checkout takes
# them in, so the two serialize instead of deadlocking.
#
# @return [Boolean] false when the code can no longer be claimed
def claim_coupon_code(discount, coupon_code)
- record = Spree::CouponCode.unused.find_by(promotion_id: discount.promotion_id, code: coupon_code)
+ record = Spree::CouponCode.find_by(promotion_id: discount.promotion_id, code: coupon_code)
return true if record.nil?
holder = record.holder
@@ -259,7 +259,7 @@ module Spree
Spree::CouponCode.transaction do
holder&.lock!
record.lock!
- next false if record.used? || !same_holder?(record.holder, holder) || checkout_in_progress?(holder)
+ next false if record.used? || !same_holder?(record.holder, holder) || !releasable?(holder)
release_coupon_code(holder, coupon_code) if holder
record.apply_order!(order)
@@ -271,12 +271,10 @@ module Spree
self.class.new(holder, enable_gift_cards: false).remove(coupon_code)
end
- def checkout_in_progress?(holder)
- case holder
- when Spree::Cart then holder.completed_at.present? || holder.completion_claimed?
- when Spree::Order then holder.completed_at.present? || holder.cart&.completion_claimed? || false
- else false
- end
+ def releasable?(holder)
+ return true if holder.nil?
+
+ holder.is_a?(Spree::Cart) && holder.completed_at.nil? && !holder.completion_claimed?
end
def same_record?(holder, other)
diff --git a/spree/core/spec/models/spree/promotion_handler/coupon_spec.rb b/spree/core/spec/models/spree/promotion_handler/coupon_spec.rb
index 980c205aae..19de1579bb 100644
--- a/spree/core/spec/models/spree/promotion_handler/coupon_spec.rb
+++ b/spree/core/spec/models/spree/promotion_handler/coupon_spec.rb
@@ -384,6 +384,20 @@ module Spree
end
end
+ context 'when a draft order holds the code' do
+ let(:draft_order) { create(:order_with_line_items, line_items_count: 3, store: store) }
+
+ before { coupon_code.apply_order!(draft_order) }
+
+ it 'keeps the code on the draft order' do
+ subject.apply
+
+ expect([subject.status_code, subject.error]).to eq([:coupon_code_used, Spree.t(:coupon_code_used)])
+ expect(coupon_code.reload.holder).to eq(draft_order)
+ expect(order.reload.total).to eq(130)
+ end
+ end
+
describe '#remove' do
before do
subject.apply