Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix: Shipping part of an order charged the customer for delivery twice

Łukasz Adamczyk51fe7fe6a27e
diff --git a/spree/core/app/workflows/spree/fulfillments/create.rb b/spree/core/app/workflows/spree/fulfillments/create.rb
index d84cbedeab..5f128ad024 100644
--- a/spree/core/app/workflows/spree/fulfillments/create.rb
+++ b/spree/core/app/workflows/spree/fulfillments/create.rb
@@ -134,13 +134,17 @@ module Spree
# Moved here from the old updater completed-order branch: pending/ready
# fulfillments re-price from backoffice-visible delivery methods;
# fulfilled ones keep their frozen cost.
+ #
+ # A caller that priced this parcel itself is honoured rather than
+ # re-quoted.
def reprice_pending_fulfillments(order)
- order.fulfillments.each do |fulfillment|
- next unless fulfillment.persisted?
- next if fulfillment.fulfilled?
+ order.fulfillments.each do |candidate|
+ next unless candidate.persisted?
+ next if candidate.fulfilled?
+ next if @requested_cost && candidate.id == fulfillment.id
- fulfillment.refresh_rates(Spree::DeliveryMethod::BACKOFFICE)
- fulfillment.update_amounts
+ candidate.refresh_rates(Spree::DeliveryMethod::BACKOFFICE)
+ candidate.update_amounts
end
end
diff --git a/spree/core/app/workflows/spree/fulfillments/fulfill.rb b/spree/core/app/workflows/spree/fulfillments/fulfill.rb
index f78383cfbc..a781c8704e 100644
--- a/spree/core/app/workflows/spree/fulfillments/fulfill.rb
+++ b/spree/core/app/workflows/spree/fulfillments/fulfill.rb
@@ -244,7 +244,10 @@ module Spree
order: @source.order,
stock_location: @source.stock_location,
items: @requested,
- delivery_method: @source.delivery_method
+ delivery_method: @source.delivery_method,
+ # Delivery was bought once, at checkout. Charging the rate again
+ # would bill the customer for the warehouse's decision.
+ cost: 0
)
failure(@source, result.error) unless result.success?
diff --git a/spree/core/spec/workflows/spree/fulfillments/create_spec.rb b/spree/core/spec/workflows/spree/fulfillments/create_spec.rb
index f8da6fa43c..0a6f2e4962 100644
--- a/spree/core/spec/workflows/spree/fulfillments/create_spec.rb
+++ b/spree/core/spec/workflows/spree/fulfillments/create_spec.rb
@@ -217,6 +217,36 @@ module Spree
expect(fulfillment.cost).to eq(original_cost)
end
+ # The specs above register the parcel as shipped, which freezes its cost.
+ # A pending one goes through the rate machinery, and the caller's price
+ # has to survive that — otherwise stating a cost does nothing.
+ context 'on a pending fulfillment' do
+ let(:params) do
+ { order: order, stock_location: stock_location, cost: '7.42' }
+ end
+
+ it 'keeps the given cost instead of re-quoting the carrier' do
+ expect(execute.success?).to eq(true)
+ expect(fulfillment.reload.cost).to eq(BigDecimal('7.42'))
+ end
+
+ # What a split shipping part of an order passes: delivery was bought
+ # once at checkout, so the second parcel carries nothing.
+ it 'keeps a zero cost, so a split parcel is free' do
+ params[:cost] = 0
+
+ expect(execute.success?).to eq(true)
+ expect(fulfillment.reload.cost).to eq(0)
+ end
+
+ it 'still prices a parcel the caller did not price' do
+ params.delete(:cost)
+
+ expect(execute.success?).to eq(true)
+ expect(fulfillment.reload.cost).to eq(fulfillment.selected_shipping_rate.cost)
+ end
+ end
+
it 'rejects a negative cost' do
params[:cost] = -5
diff --git a/spree/core/spec/workflows/spree/fulfillments/fulfill_spec.rb b/spree/core/spec/workflows/spree/fulfillments/fulfill_spec.rb
index 2a12fea22b..e8cb722bb4 100644
--- a/spree/core/spec/workflows/spree/fulfillments/fulfill_spec.rb
+++ b/spree/core/spec/workflows/spree/fulfillments/fulfill_spec.rb
@@ -68,6 +68,23 @@ module Spree
expect(source.fulfillment_items.sum(:quantity)).to eq(3)
end
+ # A second parcel charged the method's rate again, which also dropped
+ # the order to partially_paid.
+ it 'ships the split parcel without charging delivery again' do
+ partial_order.recalculate_totals!
+ before_delivery = partial_order.reload.delivery_total
+
+ execute
+
+ expect(partial_order.reload.delivery_total).not_to be > before_delivery
+ end
+
+ it 'gives the new parcel no delivery cost of its own' do
+ shipped = execute.value
+
+ expect(shipped.cost).to eq(0)
+ end
+
it 'reports the order as partially fulfilled' do
execute
expect(partial_order.reload.fulfillment_status).to eq('partial')