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 silent failure when promotion code is not yet eligible (V-3587) (#14569)

Damian Legawiecdf5e93f70f0d
diff --git a/spree/api/app/controllers/spree/api/v3/store/carts/discount_codes_controller.rb b/spree/api/app/controllers/spree/api/v3/store/carts/discount_codes_controller.rb
index cb240b96d1..d48a512d0e 100644
--- a/spree/api/app/controllers/spree/api/v3/store/carts/discount_codes_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/carts/discount_codes_controller.rb
@@ -23,6 +23,7 @@ module Spree
if coupon_handler.successful? || pending_eligibility?
@cart.save!(validate: false) if @cart.changed?
@cart.reload
+ attach_pending_coupon_warning if pending_eligibility?
render_cart(status: :created)
else
@cart.update_column(:coupon_code, nil) if @cart.read_attribute(:coupon_code).present?
@@ -58,6 +59,15 @@ module Spree
coupon_handler.status_code == :coupon_code_not_eligible
end
+ # The code is kept for Shopify-parity retry-on-recalculation, but
+ # the shopper still needs to hear why nothing applied yet.
+ def attach_pending_coupon_warning
+ @cart.warnings |= [{
+ code: coupon_handler.status_code.to_s,
+ message: coupon_handler.error.presence || Spree.t(:coupon_code_not_eligible)
+ }]
+ end
+
def permitted_params
params.permit(:code)
end
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 9716af328d..ab9fa293e0 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
@@ -60,6 +60,14 @@ RSpec.describe Spree::Api::V3::Store::Carts::DiscountCodesController, type: :con
expect(order.reload.read_attribute(:coupon_code)).to eq('big50')
expect(order.discounts.where(promotion_id: promotion.id)).to be_empty
end
+
+ it 'returns the rule message in warnings' do
+ post :create, params: { cart_id: order.prefixed_id, code: 'BIG50' }
+
+ warning = json_response['warnings'].sole
+ expect(warning['code']).to eq('coupon_code_not_eligible')
+ expect(warning['message']).to be_present
+ end
end
context 'with a multi-code promotion' do
diff --git a/spree/api/spec/integration/spree/api/v3/store/discount_codes_spec.rb b/spree/api/spec/integration/spree/api/v3/store/discount_codes_spec.rb
index e97f24316d..0d84c459f3 100644
--- a/spree/api/spec/integration/spree/api/v3/store/discount_codes_spec.rb
+++ b/spree/api/spec/integration/spree/api/v3/store/discount_codes_spec.rb
@@ -65,6 +65,28 @@ RSpec.describe 'Cart Discount Codes API', type: :request, swagger_doc: 'api-refe
expect(data['error']).to be_present
end
end
+
+ response '201', 'real code kept pending with eligibility warning' 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
+ let(:'x-spree-api-key') { api_key.token }
+ let(:'Authorization') { "Bearer #{jwt_token}" }
+ let(:body) { { code: 'BIG50' } }
+
+ schema '$ref' => '#/components/schemas/Cart'
+
+ run_test! do |response|
+ data = JSON.parse(response.body)
+ expect(data['coupon_code']).to eq('big50')
+ expect(data['discount_total']).to eq('0.0')
+ warning = data['warnings'].sole
+ expect(warning['code']).to eq('coupon_code_not_eligible')
+ expect(warning['message']).to be_present
+ end
+ end
end
end