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.

Storefront API - Fix line items data in the response after removing a coupon code (#13336)

Łukasz Adamczykc4ba5a40bebd
diff --git a/api/app/controllers/spree/api/v2/storefront/cart_controller.rb b/api/app/controllers/spree/api/v2/storefront/cart_controller.rb
index 41ec8f5db3..ff156d033d 100644
--- a/api/app/controllers/spree/api/v2/storefront/cart_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/cart_controller.rb
@@ -120,6 +120,7 @@ module Spree
result_errors = coupon_codes.count > 1 ? select_errors(coupon_codes) : select_error(coupon_codes)
if result_errors.blank?
+ spree_current_order.reload
render_serialized_payload { serialized_current_order }
else
render_error_payload(result_errors)
diff --git a/api/spec/requests/spree/api/v2/storefront/cart_spec.rb b/api/spec/requests/spree/api/v2/storefront/cart_spec.rb
index 3e8d276ab2..b1739e7abc 100644
--- a/api/spec/requests/spree/api/v2/storefront/cart_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/cart_spec.rb
@@ -775,7 +775,7 @@ describe 'API V2 Storefront Cart Spec', type: :request do
end
describe 'cart#remove_coupon_code' do
- let(:execute) { delete "/api/v2/storefront/cart/remove_coupon_code/#{coupon_code}?include=promotions", headers: headers }
+ let(:execute) { delete "/api/v2/storefront/cart/remove_coupon_code/#{coupon_code}?include=promotions,line_items", headers: headers }
include_context 'coupon codes'
@@ -857,6 +857,43 @@ describe 'API V2 Storefront Cart Spec', type: :request do
end
end
+ context 'with coupon code for item discount' do
+ let(:params) { { coupon_code: coupon_code, include: 'promotions,line_items' } }
+
+ let!(:promotion) { create(:promotion, name: '10% off', code: '10off', stores: [store]) }
+ let!(:promotion_action) { Spree::Promotion::Actions::CreateItemAdjustments.create(promotion_id: promotion.id, calculator: calculator) }
+ let(:calculator) { Spree::Calculator::PercentOnLineItem.new(preferred_percent: 10) }
+
+ let(:coupon_code) { promotion.code }
+
+ before do
+ order.coupon_code = promotion.code
+ Spree::PromotionHandler::Coupon.new(order).apply
+ order.save!
+ end
+
+ it 'changes the promo totals on order and line item' do
+ expect(order.reload.promotions).to include(promotion)
+ expect(order.promo_total).to eq(-1.0)
+ expect(order.line_items.first.reload.promo_total).to eq(-1.0)
+
+ execute
+
+ expect(json_response['data']).to have_attribute(:item_total).with_value('10.0')
+ expect(json_response['data']).to have_attribute(:ship_total).with_value('100.0')
+ expect(json_response['data']).to have_attribute(:total).with_value('110.0')
+ expect(json_response['data']).to have_attribute(:promo_total).with_value('0.0')
+ expect(json_response['data']).to have_attribute(:display_promo_total).with_value('$0.00')
+
+ expect(json_response['included']).to include(have_type('line_item').and(have_attribute(:price).with_value('10.0')))
+ expect(json_response['included']).to include(have_type('line_item').and(have_attribute(:display_price).with_value('$10.00')))
+ expect(json_response['included']).to include(have_type('line_item').and(have_attribute(:total).with_value('10.0')))
+ expect(json_response['included']).to include(have_type('line_item').and(have_attribute(:display_total).with_value('$10.00')))
+ expect(json_response['included']).to include(have_type('line_item').and(have_attribute(:promo_total).with_value('0.0')))
+ expect(json_response['included']).to include(have_type('line_item').and(have_attribute(:display_promo_total).with_value('$0.00')))
+ end
+ end
+
xcontext 'when multiple coupon codes are applied' do
let!(:promotion_with_item_adjustment) do
create(