From 4c57addb02da86d3d0dc4749eceb5ec347f5c785 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 10 Feb 2021 17:35:58 +0000 Subject: [PATCH] Move shipping fee adjustment from the order to the shipment --- app/models/spree/shipment.rb | 8 ++++++-- spec/models/spree/shipment_spec.rb | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/models/spree/shipment.rb b/app/models/spree/shipment.rb index 8883f77442..38d0f49a14 100644 --- a/app/models/spree/shipment.rb +++ b/app/models/spree/shipment.rb @@ -12,7 +12,7 @@ module Spree has_many :shipping_methods, through: :shipping_rates has_many :state_changes, as: :stateful has_many :inventory_units, dependent: :delete_all - has_one :adjustment, as: :source, dependent: :destroy + has_many :adjustments, as: :adjustable, dependent: :destroy before_create :generate_shipment_number after_save :ensure_correct_adjustment, :update_order @@ -258,6 +258,10 @@ module Spree inventory_units.create(variant_id: variant.id, state: state, order_id: order.id) end + def adjustment + @adjustment ||= adjustments.shipping.first + end + def ensure_correct_adjustment if adjustment adjustment.originator = shipping_method @@ -267,7 +271,7 @@ module Spree adjustment.reload elsif selected_shipping_rate_id shipping_method.create_adjustment(shipping_method.adjustment_label, - order, + self, self, true, "open") diff --git a/spec/models/spree/shipment_spec.rb b/spec/models/spree/shipment_spec.rb index e3c19825a5..4ebb1ff597 100644 --- a/spec/models/spree/shipment_spec.rb +++ b/spec/models/spree/shipment_spec.rb @@ -387,7 +387,7 @@ describe Spree::Shipment do it "should create adjustment when not present" do allow(shipment).to receive_messages(selected_shipping_rate_id: 1) expect(shipping_method).to receive(:create_adjustment).with(shipping_method.adjustment_label, - order, shipment, true, "open") + shipment, shipment, true, "open") shipment.__send__(:ensure_correct_adjustment) end @@ -395,7 +395,7 @@ describe Spree::Shipment do it "should use the shipping method's adjustment label" do allow(shipment).to receive_messages(selected_shipping_rate_id: 1) allow(shipping_method).to receive_messages(adjustment_label: "Foobar") - expect(shipping_method).to receive(:create_adjustment).with("Foobar", order, + expect(shipping_method).to receive(:create_adjustment).with("Foobar", shipment, shipment, true, "open") shipment.__send__(:ensure_correct_adjustment) end