Ensure order totals and payment/shipment states are correct when changing shipping method on a completed order.

This used to happen via an after_save callback in Shipment, which called `order.update!`. That has recently been removed. After changing a shipment's selected shipping rate (shipping method), we need to ensure the order's totals and states are updated. We don't need to update all of the order's adjustments though (see previous commit).

FYI Spree handled this issue like this: 24388485ed, but there are lots of things about that commit that are clearly awful, like: params handling in a model, duplication of Order::Updater logic across the codebase, the Shipment class having responsiblity for knowing which things need to be updated on the order, etc. The result is ultimately the same as what we're doing here though.
This commit is contained in:
Matt-Yorkley
2021-04-30 12:53:12 +01:00
parent 925676f136
commit b843b871f6
2 changed files with 4 additions and 7 deletions

View File

@@ -34,7 +34,9 @@ module Api
@shipment.fee_adjustment.open
end
@shipment.update(shipment_params)
if @shipment.update(shipment_params)
@order.updater.update_totals_and_states
end
if unlock == 'yes'
@shipment.fee_adjustment.close

View File

@@ -225,12 +225,7 @@ describe Api::V0::ShipmentsController, type: :controller do
expect(order.shipment.shipping_method).to eq shipping_method2
expect(order.shipment.cost).to eq 20
expect(order.total).to eq 60 # order totals have not been updated
expect(order.payment_state).to eq "paid" # payment state not updated
order.update! # Simulate "Update and recalculate fees" button
expect(order.total).to eq 70 # order total has now changed
expect(order.total).to eq 70 # item total is 50, shipping cost is 20
expect(order.payment_state).to eq "balance_due" # total changed, payment is due
end
end