Fix checking if shipping method changed

We now check if the shipping method changed before we actually select
it. Fix the related spec.The spec was wrong because order.select_shipping_method
fails silently. That means the shipping method wasn't getting
updated on the order, thus the test was passing.
This commit is contained in:
Gaetan Craig-Riou
2023-10-27 14:20:31 +11:00
parent d5d043880a
commit 33de80f13c
2 changed files with 14 additions and 3 deletions

View File

@@ -98,21 +98,25 @@ class SplitCheckoutController < ::BaseController
def update_order
return if params[:confirm_order] || @order.errors.any?
# Checking if shipping method updated before @order get updated. We can't use this guard
# clause in recalculate_voucher as by then the @order.shipping method would be the new one
shipping_method_updated = @order.shipping_method&.id != params[:shipping_method_id].to_i
@order.select_shipping_method(params[:shipping_method_id])
@order.update(order_params)
# We need to update voucher to take into account:
# * when moving away from "details" step : potential change in shipping method fees
# * when moving away from "payment" step : payment fees
recalculate_voucher if details_step? || payment_step?
recalculate_voucher(shipping_method_updated) if details_step? || payment_step?
@order.update_totals_and_states
validate_current_step
end
def recalculate_voucher
def recalculate_voucher(shipping_method_updated)
return if @order.voucher_adjustments.empty?
return if @order.shipping_method&.id == params[:shipping_method_id].to_i
return unless shipping_method_updated
VoucherAdjustmentsService.new(@order).update
end

View File

@@ -216,6 +216,13 @@ describe SplitCheckoutController, type: :controller do
end
let(:new_shipping_method) { create(:shipping_method, distributors: [distributor]) }
before do
# Add a shipping rates for the new shipping method to prevent
# order.select_shipping_method from failing
order.shipment.shipping_rates <<
Spree::ShippingRate.create(shipping_method: new_shipping_method, selected: true)
end
it "recalculates the voucher adjustment" do
expect(service).to receive(:update)