Skip to content

Commit 61e08a6

Browse files
authored
Merge pull request solidusio#6443 from SuperGoodSoft/quantity-adjustment-issue
Persist line item action quantity when updated
2 parents 0ef25ee + 1ee359f commit 61e08a6

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

legacy_promotions/app/models/spree/promotion/actions/create_quantity_adjustments.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ def set_line_item_action_quantity(quantity, line_item)
111111
line_item_action = line_item_actions.where(
112112
line_item_id: line_item.id
113113
).first_or_initialize
114+
115+
if line_item_action.persisted?
116+
line_item.line_item_actions
117+
.find { |action| action.id == line_item_action.id }
118+
.quantity = quantity
119+
end
120+
114121
line_item_action.quantity = quantity
115122
end
116123

legacy_promotions/app/models/spree/promotion/order_adjustments_recalculator.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ def recalculate(adjustment, persist:)
7070

7171
adjustment.eligible = calculate_eligibility(adjustment)
7272

73-
adjustment.save!(validate: false) if persist
73+
if persist
74+
adjustment.save!(validate: false)
75+
adjustment.adjustable.line_item_actions.find_all(&:changed?).each(&:save!) if adjustment.adjustable.respond_to?(:line_item_actions)
76+
end
7477
end
7578
adjustment.amount
7679
end

legacy_promotions/spec/models/spree/promotion/order_adjustments_recalculator_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,35 @@ def initialize(_adjustments)
1818
let(:order) { create(:order_with_line_items, line_items_count: 1, line_items_price: 10) }
1919
let(:line_item) { order.line_items[0] }
2020

21+
context "when the quantity changes with a CreateQuantityAdjustments promotion" do
22+
let(:promotion) { create(:promotion, promotion_actions: [promotion_action]) }
23+
let(:promotion_action) { Spree::Promotion::Actions::CreateQuantityAdjustments.new(calculator:, preferred_group_size: 2) }
24+
let(:calculator) { create :calculator, preferred_amount: 5 }
25+
let(:order) { create(:order_with_line_items, line_items_attributes: [{quantity: 2, price: 10}]) }
26+
27+
before do
28+
promotion.activate(order:)
29+
order.recalculate
30+
line_item.update!(quantity: 5)
31+
end
32+
33+
it "updates the promotion adjustments amount" do
34+
expect {
35+
subject
36+
}.to change {
37+
line_item.adjustments.first.amount
38+
}.from(-10).to(-20)
39+
end
40+
41+
it "updates the quantity on the line item action" do
42+
expect {
43+
subject
44+
}.to change {
45+
promotion_action.line_item_actions.reload.first.quantity
46+
}.from(2).to(4)
47+
end
48+
end
49+
2150
context "when the item quantity has changed" do
2251
let(:promotion) { create(:promotion, promotion_actions: [promotion_action]) }
2352
let(:promotion_action) { Spree::Promotion::Actions::CreateItemAdjustments.new(calculator:) }

0 commit comments

Comments
 (0)