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: 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.rbindex 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 Spreeend# 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 claimeddef 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 SpreeSpree::CouponCode.transaction doholder&.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 holderrecord.apply_order!(order)@@ -271,12 +271,10 @@ module Spreeself.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?enddef 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.rbindex 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 Spreeendend+ 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' dobefore dosubject.apply