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 four latent bugs in the renamed delivery methods

Damian Legawiec13aa63aae6e9
diff --git a/docs/plans/6.0-fulfillment-and-delivery.md b/docs/plans/6.0-fulfillment-and-delivery.md
index 6b0abaf105..0b2a251c3e 100644
--- a/docs/plans/6.0-fulfillment-and-delivery.md
+++ b/docs/plans/6.0-fulfillment-and-delivery.md
@@ -29,6 +29,13 @@ Three changes in one:
**Internal method vocabulary follows the models (2026-08-05).** Core code now calls the delivery/fulfillment names everywhere; the shipping-era names survive one release as `Spree::Deprecation.warn` bridges. Renamed: `Stock::Estimator#shipping_rates` → `#delivery_rates` (and its internals — `calculate_`/`choose_default_`/`sort_`, plus the `shipping_methods` filter seam → `#delivery_methods`, which host apps override — `#delivery_methods` dispatches through the legacy name when a subclass or decorator has redefined it, so existing overrides keep filtering until 6.1 rather than being silently skipped), `Stock::Package#shipping_rates`/`#to_shipment` → `#delivery_rates`/`#to_fulfillment`, `Stock::Coordinator#shipments` → `#fulfillments`, `Order#line_items_without_shipping_rates` → `#line_items_without_delivery_rates`, `Order#refresh_shipment_rates` → `#refresh_delivery_rates`, `Order#ensure_available_shipping_rates` → `#ensure_available_delivery_rates` (private — no bridge), and `LineItem#shipping_cost`/`#display_shipping_cost` → `#delivery_cost`/`#display_delivery_cost`. **`Spree::Cart` gets no deprecation bridges at all** — it is new in 6.0, so no legacy caller can exist; its equivalent is simply `#ensure_available_delivery_rates`.
+**Four latent bugs fixed in the renamed methods (2026-08-06).** Surfaced by review while the surrounding code was being renamed; each was reproduced before the fix and is covered by a regression spec:
+
+- **`LineItem#delivery_cost` double-counted.** `has_many :fulfillments, through: :fulfillment_items` yields one row per unit, so a fulfillment holding several of a line item's units was charged once per unit (measured: 60 instead of 30). Now `fulfillments.distinct`.
+- **`LineItem#delivery_cost` aborted on the first skippable fulfillment.** The guards used `return` inside `sum`, which exits the whole method rather than the block — a canceled fulfillment ordered before an active one zeroed out the entire line item (measured: 0 instead of 10). Now `next`.
+- **`Order#line_items_without_delivery_rates` was memoized across a destroy.** `ensure_available_delivery_rates` calls `fulfillments.destroy_all` and then re-reads the list, so the cached value described fulfillments that no longer existed. Memoization removed, and the list is captured before the destroy so the error message keeps its product names.
+- **`Fulfillment#refresh_rates` could leave nothing selected.** When the previously chosen method dropped out of the new quote (audience change, eligibility rule, zone edit), `selected_rate` came back nil and the fulfillment lost its selection. It now falls back to the estimator's own pick.
+
**Remaining for 6.0:** only the vendor axis (`belongs_to :vendor` replacing the `Spree::VendorConcern` decorator seam, `DeliveryMethodStockLocation` vendor-consistency validation, and refusing `pickup_stock_policy: 'any'` on vendor-owned locations). It is gated on `6.0-multi-vendor-marketplace.md` shipping the `vendor_id` column in its schema wave. Everything else outstanding is Phase 6 (6.1) cleanup.
### Shipped earlier (verified 2026-07-27, HEAD 4e5b6fd)
diff --git a/spree/core/app/models/spree/fulfillment.rb b/spree/core/app/models/spree/fulfillment.rb
index 6ca58d4259..cf021e36f8 100644
--- a/spree/core/app/models/spree/fulfillment.rb
+++ b/spree/core/app/models/spree/fulfillment.rb
@@ -432,13 +432,15 @@ module Spree
delivery_rates(to_package, audience)
if delivery_method
- selected_rate = delivery_rates.detect do |rate|
+ # Keep the previously chosen method when it is still quoted; otherwise
+ # fall back to the estimator's own pick (the cheapest rate, already
+ # flagged selected) rather than leaving the fulfillment unselected.
+ selected_rate =
if original_shipping_method_id
- rate.delivery_method_id == original_shipping_method_id
- else
- rate.selected
+ delivery_rates.detect { |rate| rate.delivery_method_id == original_shipping_method_id }
end
- end
+ selected_rate ||= delivery_rates.detect(&:selected)
+
save!
self.selected_shipping_rate_id = selected_rate.id if selected_rate
reload
diff --git a/spree/core/app/models/spree/line_item.rb b/spree/core/app/models/spree/line_item.rb
index 0272b41a68..8d401bfee0 100644
--- a/spree/core/app/models/spree/line_item.rb
+++ b/spree/core/app/models/spree/line_item.rb
@@ -252,17 +252,21 @@ module Spree
#
# @return [BigDecimal]
def delivery_cost
- fulfillments.sum do |fulfillment|
- return BigDecimal('0') if fulfillment.canceled?
- return BigDecimal('0') if fulfillment.cost.zero?
+ # distinct: the has_many :through yields one row per fulfillment item,
+ # so a fulfillment holding several of this line item's units would
+ # otherwise be counted once per unit.
+ fulfillments.distinct.sum do |fulfillment|
+ # next, never return — a return here would abandon the whole sum, so
+ # one skippable fulfillment would zero out every other one.
+ next BigDecimal('0') if fulfillment.canceled? || fulfillment.cost.zero?
- total_units = fulfillment.fulfillment_items
- return BigDecimal('0') if total_units.empty?
+ units = fulfillment.fulfillment_items
+ next BigDecimal('0') if units.empty?
- line_item_units = fulfillment.fulfillment_items.find_all { |unit| unit.line_item_id == id }.count
- return BigDecimal('0') if line_item_units.zero?
+ line_item_units = units.count { |unit| unit.line_item_id == id }
+ next BigDecimal('0') if line_item_units.zero?
- fulfillment.cost * (line_item_units.to_d / total_units.count)
+ fulfillment.cost * (line_item_units.to_d / units.count)
end
end
diff --git a/spree/core/app/models/spree/order.rb b/spree/core/app/models/spree/order.rb
index 9852cb7867..5af4d3cc12 100644
--- a/spree/core/app/models/spree/order.rb
+++ b/spree/core/app/models/spree/order.rb
@@ -767,9 +767,13 @@ module Spree
# Returns line items that have no delivery rates
#
+ # Deliberately not memoized: callers destroy fulfillments between reads
+ # (see {#ensure_available_delivery_rates}), so a cached value would
+ # describe fulfillments that no longer exist.
+ #
# @return [Array<Spree::LineItem>]
def line_items_without_delivery_rates
- @line_items_without_delivery_rates ||= fulfillments.map do |fulfillment|
+ fulfillments.map do |fulfillment|
fulfillment.manifest.map(&:line_item) if fulfillment.delivery_rates.blank?
end.flatten.compact
end
@@ -1033,14 +1037,18 @@ module Spree
end
def ensure_available_delivery_rates
- if fulfillments.empty? || line_items_without_delivery_rates.present?
+ # Captured before destroy_all — afterwards there are no fulfillments
+ # left to derive the undeliverable items from.
+ undeliverable_line_items = line_items_without_delivery_rates
+
+ if fulfillments.empty? || undeliverable_line_items.present?
# After this point, order redirects back to 'address' state and asks user to pick a proper address
# Therefore, shipments are not necessary at this point.
fulfillments.destroy_all
- if line_items_without_delivery_rates.present?
- errors.add(:base, Spree.t(:products_cannot_be_shipped, product_names: line_items_without_delivery_rates.map(&:name).to_sentence))
- self.warnings |= line_items_without_delivery_rates.map do |line_item|
+ if undeliverable_line_items.present?
+ errors.add(:base, Spree.t(:products_cannot_be_shipped, product_names: undeliverable_line_items.map(&:name).to_sentence))
+ self.warnings |= undeliverable_line_items.map do |line_item|
{
code: 'delivery_unavailable',
message: Spree.t('cart_line_item.delivery_unavailable', li_name: line_item.name),
diff --git a/spree/core/spec/models/spree/line_item_spec.rb b/spree/core/spec/models/spree/line_item_spec.rb
index da130bbdca..8dddb53386 100644
--- a/spree/core/spec/models/spree/line_item_spec.rb
+++ b/spree/core/spec/models/spree/line_item_spec.rb
@@ -615,6 +615,26 @@ describe Spree::LineItem, type: :model do
expect(line_item.delivery_cost).to eq(0)
end
end
+
+ context 'when one fulfillment holds several of this line item units' do
+ it 'counts that fulfillment once, not once per unit' do
+ second_unit = create(:inventory_unit, line_item: line_item, variant: inventory_unit.variant)
+ shipment.fulfillment_items << second_unit
+
+ expect(line_item.reload.delivery_cost).to eq(10)
+ end
+ end
+
+ context 'when a canceled fulfillment precedes an active one' do
+ it 'still charges the active fulfillment' do
+ shipment.update_columns(cost: 10, status: 'canceled')
+
+ active = create(:shipment, cost: 10, order: shipment.order, stock_location: shipment.stock_location)
+ active.fulfillment_items << create(:inventory_unit, line_item: line_item, variant: inventory_unit.variant)
+
+ expect(line_item.reload.delivery_cost).to eq(10)
+ end
+ end
end
describe '#amount' do
diff --git a/spree/core/spec/models/spree/order_spec.rb b/spree/core/spec/models/spree/order_spec.rb
index 117b48cc77..0a9b5c1d51 100644
--- a/spree/core/spec/models/spree/order_spec.rb
+++ b/spree/core/spec/models/spree/order_spec.rb
@@ -2169,6 +2169,14 @@ describe Spree::Order, type: :model do
it 'deletes all the shipments' do
expect { subject }.to change(order.shipments, :count).to(0)
end
+
+ # The list is derived from fulfillments, and this method destroys them —
+ # so it must be read fresh, never from a value cached across the destroy.
+ it 'reports no undeliverable line items once the fulfillments are gone' do
+ expect(order.line_items_without_delivery_rates).to be_present
+ subject
+ expect(order.line_items_without_delivery_rates).to be_empty
+ end
end
context 'when order has shipments with shipping rates' do
diff --git a/spree/core/spec/models/spree/shipment_spec.rb b/spree/core/spec/models/spree/shipment_spec.rb
index b696470adc..829384608a 100644
--- a/spree/core/spec/models/spree/shipment_spec.rb
+++ b/spree/core/spec/models/spree/shipment_spec.rb
@@ -501,6 +501,20 @@ describe Spree::Shipment, type: :model do
expect(shipment.reload.selected_shipping_rate).not_to be_nil
end
+ # The previously chosen method can drop out of the new quote (audience
+ # change, eligibility rule, zone edit). The fulfillment must fall back to
+ # the estimator's own pick rather than end up with nothing selected.
+ it 'falls back to the estimator default when the original method is no longer quoted' do
+ dropped_method = create(:shipping_method)
+ expect(Spree::Stock::Estimator).to receive(:new).with(shipment.order).and_return(mock_estimator)
+ allow(shipment).to receive_messages(delivery_method: dropped_method)
+
+ shipment.refresh_rates
+
+ expect(shipment.reload.selected_shipping_rate).not_to be_nil
+ expect(shipment.selected_shipping_rate.shipping_method_id).to eq(shipping_method1.id)
+ end
+
it 'does not refresh if shipment is shipped' do
expect(Spree::Stock::Estimator).not_to receive(:new)
shipment.shipping_rates.delete_all