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: Carts preselect Store Pickup before an address is entered (V-3718) (#14788)

Łukasz Adamczyk1aa9de6c1e31
diff --git a/docs/developer/core-concepts/carts.mdx b/docs/developer/core-concepts/carts.mdx
index f02cacf99c..5af59907e6 100644
--- a/docs/developer/core-concepts/carts.mdx
+++ b/docs/developer/core-concepts/carts.mdx
@@ -235,6 +235,8 @@ To add your own step or requirement, see
<Step title="Choose delivery">
Each [fulfillment](/developer/core-concepts/fulfillments) offers delivery rates. Pick one per fulfillment.
+ A cart has no fulfillments until it has a shipping address or a pickup location, unless none of its items ships to an address (a digital-only cart has them straight away). Saving a different address replaces the fulfillments, under new ids, with the cheapest shipping rate selected, so show the customer the new options before taking payment.
+
```typescript Store SDK
const cart = await client.carts.get(cartId)
await client.carts.fulfillments.update(cartId, cart.fulfillments[0].id, {
diff --git a/docs/plans/6.0-fulfillment-and-delivery.md b/docs/plans/6.0-fulfillment-and-delivery.md
index 704cc2156d..817b87bc35 100644
--- a/docs/plans/6.0-fulfillment-and-delivery.md
+++ b/docs/plans/6.0-fulfillment-and-delivery.md
@@ -251,7 +251,7 @@ Digital and both pickup kinds ARE deliveries — they just don't ship to a custo
**Resolved 2026-08-02 — pickup flow correctness (shipped together as one unit):**
-- **Rebuild triggers**: a destination is an address OR a pickup intent. `Carts::Update#try_advance` rebuilds proposals on `saved_change_to_preferred_stock_location_id?` (set, changed, or cleared) exactly like address/market changes; `Checkout::Advance` proposes fulfillments when `ship_address.present? || preferred_stock_location_id.present?`. A pure-pickup cart completes checkout without ever having a shipping address.
+- **Rebuild triggers**: a destination is an address OR a pickup intent. `Carts::Update#try_advance` rebuilds proposals on `saved_change_to_preferred_stock_location_id?` (set, changed, or cleared) exactly like address/market changes. Whether anything is proposed is one rule, `Purchase::Addresses#can_propose_deliveries?` — a shipping address, a pickup intent, or items no delivery profile ships to an address — read by both `Checkout::Advance` and every `Cart#rebuild_fulfillments!`, including the one after an item change. Until it holds the cart has no proposals, so the storefront shows no delivery option before the address (2026-10-01, V-3718). A pure-pickup cart completes checkout without ever having a shipping address.
- **Estimator location gating**: `Stock::Estimator#shipping_methods` filters on `DeliveryMethod#serves_location?(package.stock_location)`, delegated to the fulfillment provider. Base → true (shipping/digital/pickup_point are source-agnostic); `FulfillmentProvider::Pickup` → the package must be sourced from an eligible pickup location (`available_pickup_locations`: the configured `DeliveryMethodStockLocation` set, else all active pickup-enabled), **or** some eligible location must accept remote stock (`pickup_stock_policy: 'any'` — the ship-to-store case, which a strict source gate would otherwise outlaw). This makes `DeliveryMethodStockLocation` load-bearing in rating, not just in the storefront listing endpoint.
- **Coverage checks are pickup-item-scoped**: the store `pickup_locations?cart_id=` coverage check skips line items whose product `fulfillment_types` exclude `pickup` — a tracked digital variant in a mixed cart must not disqualify a counter.
- Cart-side sourcing still comes from the Coordinator across all stocked locations; honoring the *chosen* counter remains order-side routing (`PreferredLocation` rule) per "Pickup location selection flows through order routing" above.
diff --git a/docs/plans/decisions.md b/docs/plans/decisions.md
index 5bbb4e0ff0..12c7a45c4b 100644
--- a/docs/plans/decisions.md
+++ b/docs/plans/decisions.md
@@ -1,3 +1,15 @@
+## 2026-10-01: No delivery is offered before the address, and the storefront never charges a delivery it has not shown
+
+**Context:** The storefront charged a delivery method and total the buyer was never shown (V-3718). Reproduced on `main`: a signed-in buyer's checkout read Store Pickup $0.00, total $89.98; pressing Place Order booked order R1001 as Standard $5.00, $94.98, and the Check payment completed for $94.98. Two things combined. Every item change rebuilt the cart's delivery proposals whether or not it had a destination, and quoted without one, only pickup qualifies, so the estimator preselected it. That contradicted the 2026-08-02 rule that proposals wait for an address or a pickup intent, which only `Checkout::Advance` applied. The preselected pickup also made `shipping_address_required?` answer false, so a cart whose email had saved but whose address had not could complete as pickup without the buyer choosing it. Then pressing Place Order moved focus out of the address block, which saved the address. That rebuilt the proposals for the new destination and picked Standard, as the 2026-09-24 entry intends. Next.js runs a client's server actions one at a time, so the Place Order handler always re-read the cart after that save, and then paid without comparing it with what was on screen.
+
+**Decision:** Whether a cart gets delivery proposals is one rule, `Purchase::Addresses#can_propose_deliveries?`: a shipping address, a pickup intent, or items that no delivery profile ships to an address (digital, or pickup-only profiles). `Cart#rebuild_fulfillments!` and `Checkout::Advance` both read it, so a cart that needs an address shows no delivery option until it has one, as Shopify's checkout does. The rule reuses the item half of `shipping_address_required?`, now extracted as `items_require_shipping_address?`. In the storefront, Place Order first waits for any address save the press itself started, rather than counting on Next.js running server actions one at a time, and stops if that save failed, because the cart still holds the old address. It then compares the delivery that was on screen at the press with the cart it re-reads. The on-screen delivery is captured on pointerdown, before the address block's blur save can re-render it, and compared as each shipment's selected option name and price, not ids, because a rebuild that keeps the choice (after a corrected phone number) issues new ids. If they differ, it pays nothing, puts "Choose a delivery method, then place your order again." on the delivery section and focuses the selected option. The check runs before payment, so it also covers payment sessions charged ahead of completion.
+
+Rejected: sending the client's `expected_total` with completion (the `Carts::Complete` drift guard, which the Store API's complete endpoint does not pass through). A total cannot tell two methods at the same price apart, and a guard at completion comes after a payment session has already charged. Rejected: saving the address as soon as its last required field is filled. It would make the stop rarer, but a buyer can still type the ZIP and press straight away. Rejected: hiding delivery only in the storefront. The server would still preselect pickup and still consider such carts in need of no address.
+
+**Consequences:** Before an address, the Store API cart has no fulfillments, and its requirements now list "Shipping address is required" and "Select a delivery method for all fulfillments". The storefront's existing "Enter your shipping address to view available shipping methods." message and the summary's "Enter shipping address" shipping line show again. A store whose items only offer pickup still gets its pickup proposal straight away. Carts built before this change keep their pre-address pickup proposal until their next rebuild; if that rebuild happens on the press, the storefront stop catches it. Draft orders are unchanged: `Order#rebuild_fulfillments!` does not read the rule, and admin draft-order building was out of scope. The `expected_total` drift guard stays unreachable from the Store API. Left open: a total that moves while the delivery stays the same, such as tax after a country change saved by the press, is not stopped; a buyer who wants pickup must enter an address first unless the client sets a pickup intent (`preferred_stock_location_id`), which the starter storefront has no control for; and `DeliveryProfile#requires_shipping_address?` counts back-office-only methods, so a profile whose only storefront method is pickup but which also has an admin-only courier waits for an address.
+
+**Plans amended:** `6.0-fulfillment-and-delivery.md` (Rebuild triggers).
+
## 2026-09-30: Merchants edit email templates in the dashboard, as drafts published per store
**Context:** Since the Liquid and MJML plan, every email renders from a template only a developer can change. The end goal was always merchant editing; this settles its shape.
diff --git a/spree/core/app/models/concerns/spree/purchase/addresses.rb b/spree/core/app/models/concerns/spree/purchase/addresses.rb
index 1f81951f26..1a79bcb13c 100644
--- a/spree/core/app/models/concerns/spree/purchase/addresses.rb
+++ b/spree/core/app/models/concerns/spree/purchase/addresses.rb
@@ -59,17 +59,17 @@ module Spree
return false if preferred_stock_location_id.present?
- # An address is needed when any item's profile ships to one —
- # answered by the profile kind, resolved once per profile.
- profile_requires_address = Hash.new do |cache, profile|
- cache[profile] = profile.present? && profile.requires_shipping_address?
- end
+ items_require_shipping_address?
+ end
- # Both profile paths are preloaded: the variant's own override, and the
- # product's for the variants that inherit it.
- line_items.includes(variant: [:delivery_profile, { product: :delivery_profile }]).any? do |line_item|
- profile_requires_address[line_item.variant&.resolved_delivery_profile]
- end
+ # Whether delivery can be proposed yet: once there is a destination (a
+ # shipping address, or a pickup intent for a purchase that never gets
+ # one), or straight away when no item ships to an address. Quoted
+ # without a destination, only pickup qualifies.
+ #
+ # @return [Boolean]
+ def can_propose_deliveries?
+ ship_address.present? || preferred_stock_location_id.present? || !items_require_shipping_address?
end
# @deprecated The shipping address is canonical — use +use_shipping+ to
@@ -174,6 +174,19 @@ module Spree
private
+ def items_require_shipping_address?
+ # Answered by the profile kind, resolved once per profile.
+ profile_requires_address = Hash.new do |cache, profile|
+ cache[profile] = profile.present? && profile.requires_shipping_address?
+ end
+
+ # Both profile paths are preloaded: the variant's own override, and the
+ # product's for the variants that inherit it.
+ line_items.includes(variant: [:delivery_profile, { product: :delivery_profile }]).any? do |line_item|
+ profile_requires_address[line_item.variant&.resolved_delivery_profile]
+ end
+ end
+
# The address behind an id a client may select, or nil when the id names
# one this purchase has no business shipping to.
#
diff --git a/spree/core/app/models/spree/cart.rb b/spree/core/app/models/spree/cart.rb
index 2167b47352..5571dc1a46 100644
--- a/spree/core/app/models/spree/cart.rb
+++ b/spree/core/app/models/spree/cart.rb
@@ -161,11 +161,13 @@ module Spree
# Idempotent delivery-proposal rebuild — replaces the destructive
# order-side create_proposed_fulfillments. Open fulfillments are rebuilt from
- # the current items/address; nothing here touches a completed cart.
+ # the current items/address; nothing here touches a completed cart. Until
+ # deliveries can be proposed the cart gets none: preselecting the pickup
+ # rate that is all a destination-less quote finds would choose pickup for
+ # a customer who had not been offered anything.
#
# @param keep_selection [Boolean] false when the destination changed: a
- # choice made for one address is not a choice for another, and before an
- # address is known the only rate on offer is a pickup default
+ # choice made for one address is not a choice for another
def rebuild_fulfillments!(keep_selection: true)
return if completed?
@@ -179,6 +181,7 @@ module Spree
DeliveryRate.where(fulfillment_id: fulfillment_ids).delete_all
fulfillments.delete_all
fulfillment_items.reset
+ return fulfillments.reload unless can_propose_deliveries?
# Appended rather than assigned: setting `cart` on each proposal already
# files it under this cart's `fulfillments` (the association is
diff --git a/spree/core/app/services/spree/checkout/advance.rb b/spree/core/app/services/spree/checkout/advance.rb
index 2602ee68aa..dd139a07c9 100644
--- a/spree/core/app/services/spree/checkout/advance.rb
+++ b/spree/core/app/services/spree/checkout/advance.rb
@@ -11,10 +11,7 @@ module Spree
def call(order:, state: nil, shipping_method_id: nil)
cart = order
- # A destination is either a shipping address or a pickup intent — a
- # pure-pickup cart never gets an address, but still needs proposals.
- has_destination = cart.ship_address.present? || cart.preferred_stock_location_id.present?
- if cart.fulfillments.empty? && cart.delivery_step_required? && has_destination && cart.respond_to?(:rebuild_fulfillments!)
+ if cart.fulfillments.empty? && cart.delivery_step_required? && cart.can_propose_deliveries? && cart.respond_to?(:rebuild_fulfillments!)
cart.rebuild_fulfillments!
end
diff --git a/spree/core/spec/models/spree/cart_spec.rb b/spree/core/spec/models/spree/cart_spec.rb
index 8467b760c6..2ed347104a 100644
--- a/spree/core/spec/models/spree/cart_spec.rb
+++ b/spree/core/spec/models/spree/cart_spec.rb
@@ -457,6 +457,43 @@ describe Spree::Cart, type: :model do
cart.update_columns(completed_at: Time.current)
expect(Spree::Cart.find(cart.id).rebuild_fulfillments!).to be_nil
end
+
+ context 'without a destination' do
+ let(:cart) { create(:cart_with_line_items, store: store, email: '[email protected]') }
+ let!(:pickup_location) { create(:stock_location, pickup_enabled: true, pickup_stock_policy: 'any', store: store) }
+
+ before { create(:pickup_delivery_method, store: store) }
+
+ it 'proposes nothing rather than preselecting pickup' do
+ cart.rebuild_fulfillments!
+
+ expect(cart.fulfillments).to be_empty
+ expect(cart.shipping_address_required?).to be(true)
+ end
+
+ it 'proposes nothing when an item is added' do
+ Spree.cart_add_item_workflow.call(cart: cart, variant: cart.line_items.first.variant, quantity: 1)
+
+ expect(cart.fulfillments.reload).to be_empty
+ end
+
+ it 'proposes pickup once the customer chooses a pickup location' do
+ cart.update!(preferred_stock_location_id: pickup_location.id)
+
+ cart.rebuild_fulfillments!
+
+ expect(cart.fulfillments.first.selected_delivery_rate.delivery_method).to be_pickup
+ end
+
+ it 'proposes straight away when no item ships to an address' do
+ cart.line_items.first.update!(variant: create(:digital_product, store: store).default_variant)
+ create(:digital_delivery_method, store: store)
+
+ cart.rebuild_fulfillments!
+
+ expect(cart.fulfillments).to be_present
+ end
+ end
end
describe '#prune_undeliverable_fulfillments!' do