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.

Report ineligible coupons as retryable in the coupon handler

Damian Legawiec81500678b4a0
diff --git a/spree/api/spec/controllers/spree/api/v3/store/carts/discount_codes_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/carts/discount_codes_controller_spec.rb
index f10b92fe15..9716af328d 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/carts/discount_codes_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/carts/discount_codes_controller_spec.rb
@@ -46,6 +46,22 @@ RSpec.describe Spree::Api::V3::Store::Carts::DiscountCodesController, type: :con
end
end
+ context 'with a real code the cart does not qualify for yet' do
+ let!(:promotion) do
+ create(:promotion_with_item_total_rule, :with_line_item_adjustment,
+ code: 'BIG50', kind: :coupon_code, store: store,
+ item_total_threshold_amount: 1_000_000, adjustment_rate: 50)
+ end
+
+ it 'keeps the code pending without discount rows' do
+ post :create, params: { cart_id: order.prefixed_id, code: 'BIG50' }
+
+ expect(response).to have_http_status(:created)
+ expect(order.reload.read_attribute(:coupon_code)).to eq('big50')
+ expect(order.discounts.where(promotion_id: promotion.id)).to be_empty
+ end
+ end
+
context 'with a multi-code promotion' do
let!(:promotion) do
create(:promotion, :with_line_item_adjustment, multi_codes: true, number_of_codes: 1)
diff --git a/spree/core/app/models/spree/promotion_handler/coupon.rb b/spree/core/app/models/spree/promotion_handler/coupon.rb
index 6af1e1fb4f..830b462ba0 100644
--- a/spree/core/app/models/spree/promotion_handler/coupon.rb
+++ b/spree/core/app/models/spree/promotion_handler/coupon.rb
@@ -155,6 +155,10 @@ module Spree
return promotion_usage_limit_exceeded if promotion.usage_limit_exceeded?(order)
unless promotion.eligible?(order, options)
+ # Rules that explain themselves keep their message, but the status
+ # code is always :coupon_code_not_eligible so callers can park the
+ # code for retry on recalculation.
+ self.status_code = :coupon_code_not_eligible
self.error = promotion.eligibility_errors.full_messages.first unless promotion.eligibility_errors.blank?
return (error || ineligible_for_this_order)
end