Skip to content

Commit 53cbf58

Browse files
adammathysjarednormanNoah-SilveraAlistairNorman
authored andcommitted
Ignore dependent records marked for destruction
When computing item totals after a line item benefit is removed, we need to ensure we aren't using the marked for destruction items. We also needed to make a change to the legacy promotion order updater patch because it is incorrectly being used in our tests even when using the new promotion system. It also did not have logic to ensure adjustments that were marked for destruction were ignored in the adjustment totals calculation. Co-authored-by: Jared Norman <jared@super.gd> Co-authored-by: Noah Silvera <noah@super.gd> Co-authored-by: Alistair Norman <alistair@super.gd>
1 parent ac02030 commit 53cbf58

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

core/app/models/spree/order_updater.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def recalculate_order_total
168168
def update_adjustment_total
169169
update_adjustments
170170

171-
all_items = line_items + shipments
171+
all_items = (line_items + shipments).reject(&:marked_for_destruction?)
172172
# Ignore any adjustments that have been marked for destruction in our
173173
# calculations. They'll get removed when/if we persist the order.
174174
valid_adjustments = adjustments.reject(&:marked_for_destruction?)

legacy_promotions/app/patches/models/solidus_legacy_promotions/spree_order_updater_patch.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module SpreeOrderUpdaterPatch
55
def update_adjustment_total
66
update_adjustments
77

8-
all_items = line_items + shipments
8+
all_items = (line_items + shipments).reject(&:marked_for_destruction?)
99
order_tax_adjustments = adjustments.select(&:eligible?).select(&:tax?)
1010

1111
order.adjustment_total = all_items.sum(&:adjustment_total) + adjustments.select(&:eligible?).sum(&:amount)
@@ -25,10 +25,11 @@ def recalculate_item_totals
2525
# Core doesn't have "eligible" adjustments anymore, so we need to
2626
# override the adjustment_total calculation to exclude them for legacy
2727
# promotions.
28-
item.adjustment_total = item.adjustments.
29-
select(&:eligible?).
30-
reject(&:included?).
31-
sum(&:amount)
28+
item.adjustment_total = item.adjustments.select { |adjustment|
29+
adjustment.eligible? &&
30+
!adjustment.marked_for_destruction? &&
31+
!adjustment.included?
32+
}.sum(&:amount)
3233

3334
if item.changed?
3435
item.update_columns(

promotions/app/models/solidus_promotions/order_adjuster.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ def call
2525
order.reset_current_discounts
2626

2727
unless dry_run
28-
# Since automations might have added a line item, we need to recalculate item total and item count here.
29-
order.item_total = order.line_items.sum(&:amount)
30-
order.item_count = order.line_items.sum(&:quantity)
31-
order.promo_total = (order.line_items + order.shipments).sum(&:promo_total)
28+
# Since automations might have added a line item, we need to recalculate
29+
# item total and item count here.
30+
line_items = order.line_items.reject(&:marked_for_destruction?)
31+
order.item_total = line_items.sum(&:amount)
32+
order.item_count = line_items.sum(&:quantity)
33+
order.promo_total = (line_items + order.shipments).sum(&:promo_total)
3234
end
3335
order
3436
end

0 commit comments

Comments
 (0)