Skip to content

Commit 975cb36

Browse files
forkatamamhoffAlistairNorman
committed
Add price sync logic to in-memory updater
This was introduced upstream in #6399 and we want to preserve this functionality in the in-memory updater we're introducing as well. Co-authored-by: Martin Meyerhoff <mamhoff@gmail.com> Co-authored-by: Alistair Norman <alistair@super.gd>
1 parent f615ea1 commit 975cb36

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

core/app/models/spree/in_memory_order_updater.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def recalculate(persist: true)
3333

3434
order.transaction do
3535
monitor.call do
36+
recalculate_line_item_prices
3637
recalculate_item_count
3738
assign_shipment_amounts
3839
end
@@ -228,6 +229,12 @@ def recalculate_item_total
228229
recalculate_order_total
229230
end
230231

232+
def recalculate_line_item_prices
233+
if Spree::Config.recalculate_cart_prices
234+
line_items.each(&:recalculate_price)
235+
end
236+
end
237+
231238
def persist_totals
232239
shipments.each(&:persist_amounts)
233240
assign_item_totals

core/spec/models/spree/in_memory_order_updater_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,39 @@ module Spree
350350
end
351351
end
352352

353+
describe "price recalculation" do
354+
let(:variant) { create(:variant, price: 98) }
355+
let!(:order) { create(:order_with_line_items, line_items_attributes: [{ variant:, price: 98 }]) }
356+
357+
subject { updater.recalculate }
358+
359+
before do
360+
variant.price = 100
361+
variant.save!
362+
order.reload
363+
end
364+
365+
context "when recalculate_cart_prices is true" do
366+
before do
367+
stub_spree_preferences(recalculate_cart_prices: true)
368+
end
369+
370+
it "sets prices to the currently active price" do
371+
expect { subject }.to change { order.line_items.first.price }.from(98).to(100)
372+
end
373+
end
374+
375+
context "when recalculate_cart_prices is false" do
376+
before do
377+
stub_spree_preferences(recalculate_cart_prices: false)
378+
end
379+
380+
it "does not recalculate prices" do
381+
expect { subject }.not_to change { order.line_items.first.price }.from(98)
382+
end
383+
end
384+
end
385+
353386
context "updating shipment state" do
354387
let(:order) { create :order_with_line_items }
355388

0 commit comments

Comments
 (0)