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.

Use coupon codes when applying promotions, fix removing coupon codes (#12190)

Łukasz Adamczykf111a3f797d2
diff --git a/core/app/models/spree/coupon_code.rb b/core/app/models/spree/coupon_code.rb
index 574b4aaadd..9e9feea30b 100644
--- a/core/app/models/spree/coupon_code.rb
+++ b/core/app/models/spree/coupon_code.rb
@@ -24,7 +24,11 @@ module Spree
end
def apply_order!(order)
- update(order: order)
+ update(order: order, state: 'used')
+ end
+
+ def remove_from_order
+ update(order: nil, state: 'unused')
end
def display_code
diff --git a/core/app/models/spree/promotion_handler/coupon.rb b/core/app/models/spree/promotion_handler/coupon.rb
index da86635b49..b28a68cb03 100644
--- a/core/app/models/spree/promotion_handler/coupon.rb
+++ b/core/app/models/spree/promotion_handler/coupon.rb
@@ -31,6 +31,11 @@ module Spree
# Order promotion has to be destroyed before line item removing
order.order_promotions.where(promotion_id: promotion.id).destroy_all
+ if promotion.multi_codes?
+ coupon_code = promotion.coupon_codes.find_by(order: order)
+ coupon_code&.remove_from_order
+ end
+
remove_promotion_adjustments(promotion)
remove_promotion_line_items(promotion)
order.update_with_updater!
@@ -82,9 +87,9 @@ module Spree
end
def handle_present_promotion
+ return promotion_applied if promotion_exists_on_order?
return set_error_code :coupon_code_used if promotion.coupon_codes.used.where(code: order.coupon_code).exists?
return promotion_usage_limit_exceeded if promotion.usage_limit_exceeded?(order)
- return promotion_applied if promotion_exists_on_order?
unless promotion.eligible?(order, options)
self.error = promotion.eligibility_errors.full_messages.first unless promotion.eligibility_errors.blank?
diff --git a/core/spec/models/spree/promotion_handler/coupon_spec.rb b/core/spec/models/spree/promotion_handler/coupon_spec.rb
index 557af067a6..f497e4aebf 100644
--- a/core/spec/models/spree/promotion_handler/coupon_spec.rb
+++ b/core/spec/models/spree/promotion_handler/coupon_spec.rb
@@ -319,6 +319,10 @@ module Spree
expect(order.total).to eq(130)
subject.apply
expect(subject.success).to be_present
+
+ expect(coupon_code.reload).to be_used
+ expect(coupon_code.order).to eq(order)
+
order.line_items.each do |line_item|
expect(line_item.adjustments.count).to eq(1)
end
@@ -342,6 +346,28 @@ module Spree
expect(subject.error).to eq Spree.t(:coupon_code_used)
end
end
+
+ describe '#remove' do
+ before do
+ subject.apply
+ order.reload
+ end
+
+ it 'removes the promotion' do
+ expect(order.total).to eq(100)
+
+ subject.remove(coupon_code.code)
+ expect(subject.success).to be_present
+
+ expect(order.reload.total).to eq(130)
+
+ expect(coupon_code.reload).to be_unused
+ expect(coupon_code.order).to be_nil
+
+ expect(order.promotions).to be_empty
+ expect(order.line_item_adjustments).to be_empty
+ end
+ end
end
end
@@ -401,7 +427,10 @@ module Spree
expect(subject.successful?).to be true
# Simulate the order has been completed so the coupon code is marked as used
Spree::CouponCodes::CouponCodesHandler.new(order: order).use_all_codes
- expect(Spree::CouponCode.find_by(code: coupon_code).state).to eq 'used'
+
+ coupon = Spree::CouponCode.find_by(code: coupon_code)
+ expect(coupon.state).to eq 'used'
+ expect(coupon.order).to eq order
subject_2.apply
expect(subject_2.successful?).to be false
@@ -418,14 +447,20 @@ module Spree
expect(subject.successful?).to be true
# Simulate the order has been completed so the coupon code is marked as used
Spree::CouponCodes::CouponCodesHandler.new(order: order).use_all_codes
- expect(Spree::CouponCode.find_by(code: 'first_one_time_code').state).to eq 'used'
+
+ coupon = Spree::CouponCode.find_by(code: 'first_one_time_code')
+ expect(coupon.state).to eq 'used'
+ expect(coupon.order).to eq order
order_2.coupon_code = 'second_one_time_code'
subject_2.apply
expect(subject_2.successful?).to be true
# Simulate the order has been completed so the coupon code is marked as used
Spree::CouponCodes::CouponCodesHandler.new(order: order_2).use_all_codes
- expect(Spree::CouponCode.find_by(code: 'second_one_time_code').state).to eq 'used'
+
+ coupon = Spree::CouponCode.find_by(code: 'second_one_time_code')
+ expect(coupon.state).to eq 'used'
+ expect(coupon.order).to eq order_2
end
end
end