diff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rb index 331613c6f2..3f165c89a3 100644 --- a/core/app/models/spree/shipment.rb +++ b/core/app/models/spree/shipment.rb @@ -262,7 +262,9 @@ def tax_total def to_package package = Stock::Package.new(stock_location) package.shipment = self - inventory_units.includes(variant: :product).joins(:variant).group_by(&:state).each do |state, state_inventory_units| + # Canceled units must not contribute to shipping rate calculation: every + # caller of this method feeds the result to the shipping estimator/calculators. + inventory_units.not_canceled.includes(variant: :product).joins(:variant).group_by(&:state).each do |state, state_inventory_units| package.add_multiple state_inventory_units, state.to_sym end package diff --git a/core/spec/models/spree/shipment_spec.rb b/core/spec/models/spree/shipment_spec.rb index 71f9893f2a..c7fbacf679 100644 --- a/core/spec/models/spree/shipment_spec.rb +++ b/core/spec/models/spree/shipment_spec.rb @@ -304,7 +304,7 @@ before do allow(line_item).to receive(:order) { order } shipment.inventory_units = inventory_units - allow(shipment.inventory_units).to receive_message_chain(:includes, :joins).and_return inventory_units + allow(shipment.inventory_units).to receive_message_chain(:not_canceled, :includes, :joins).and_return inventory_units end it "should use symbols for states when adding contents to package" do @@ -316,6 +316,20 @@ it "should set the shipment to itself" do expect(shipment.to_package.shipment).to eq(shipment) end + + context "with a canceled inventory unit on the shipment" do + let(:order_with_units) { create(:order_ready_to_ship, line_items_count: 2) } + let(:real_shipment) { order_with_units.shipments.first } + + it "excludes canceled inventory units from the package" do + kept_unit, canceled_unit = real_shipment.inventory_units.to_a + canceled_unit.update_columns(state: "canceled") + + package = real_shipment.to_package + + expect(package.contents.map(&:inventory_unit)).to contain_exactly(kept_unit) + end + end end end end