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.

Make the pickup checkout flow reachable

Damian Legawiecfd6a983b598f
diff --git a/spree/api/app/controllers/spree/api/v3/store/delivery_methods_controller.rb b/spree/api/app/controllers/spree/api/v3/store/delivery_methods_controller.rb
index 17b5fa4712..7a739319e0 100644
--- a/spree/api/app/controllers/spree/api/v3/store/delivery_methods_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/delivery_methods_controller.rb
@@ -67,10 +67,14 @@ module Spree
# 'local' policy: every cart item must be on hand at the location.
# 'any' policy: the location offers pickup regardless of local stock
# (the merchant transfers goods in).
+ # Only pickup-collectable items count toward local coverage — a
+ # tracked digital variant in a mixed cart is delivered by its own
+ # provider and must not disqualify a counter.
def covers_cart?(location, cart)
return true if location.pickup_stock_policy == 'any'
- cart.line_items.includes(:variant).all? do |line_item|
+ cart.line_items.includes(variant: :product).all? do |line_item|
+ next true unless line_item.variant.product.fulfillment_types.include?('pickup')
next true unless line_item.variant.should_track_inventory?
stock_item = location.stock_item(line_item.variant)
diff --git a/spree/core/app/services/spree/carts/update.rb b/spree/core/app/services/spree/carts/update.rb
index e9cc90c5a4..e6ef48c9dd 100644
--- a/spree/core/app/services/spree/carts/update.rb
+++ b/spree/core/app/services/spree/carts/update.rb
@@ -35,8 +35,13 @@ module Spree
attr_reader :cart, :params
- def address_changed?
- cart.saved_change_to_ship_address_id? || cart.saved_change_to_market_id?
+ # Pickup intent counts as a destination change: proposals priced for
+ # shipping (or for another counter) are stale once the customer picks
+ # or clears a pickup location.
+ def destination_changed?
+ cart.saved_change_to_ship_address_id? ||
+ cart.saved_change_to_market_id? ||
+ cart.saved_change_to_preferred_stock_location_id?
end
def assign_cart_attributes
@@ -152,7 +157,7 @@ module Spree
def try_advance
return if cart.complete?
- if @address_invalidated || address_changed?
+ if @address_invalidated || destination_changed?
cart.recalculate_for_address_change!
else
cart.recalculate_totals!
diff --git a/spree/core/spec/models/spree/stock/estimator_spec.rb b/spree/core/spec/models/spree/stock/estimator_spec.rb
index e38bebf892..cd5e30db8c 100644
--- a/spree/core/spec/models/spree/stock/estimator_spec.rb
+++ b/spree/core/spec/models/spree/stock/estimator_spec.rb
@@ -52,6 +52,30 @@ module Spree
it_behaves_like "shipping rate doesn't match"
end
+ context 'pickup methods are gated on the package source location' do
+ let(:shipping_method) { create(:pickup_delivery_method) }
+
+ context 'when the package is sourced from a plain warehouse' do
+ it_behaves_like "shipping rate doesn't match"
+ end
+
+ context 'when the package is sourced from a pickup-enabled location' do
+ let(:package) do
+ build(:stock_package,
+ stock_location: create(:stock_location, pickup_enabled: true),
+ contents: inventory_units.map { |_i| ContentItem.new(inventory_unit) })
+ end
+
+ it_behaves_like 'shipping rate matches'
+ end
+
+ context 'when a ship-to-store counter accepts remote stock' do
+ let!(:any_policy_counter) { create(:stock_location, pickup_enabled: true, pickup_stock_policy: 'any') }
+
+ it_behaves_like 'shipping rate matches'
+ end
+ end
+
context 'when the calculator is not available for that order' do
before { allow_any_instance_of(ShippingMethod).to receive_message_chain(:calculator, :available?).and_return(false) }
diff --git a/spree/core/spec/services/spree/carts/update_spec.rb b/spree/core/spec/services/spree/carts/update_spec.rb
index af5b33f2f5..e3c62a18fb 100644
--- a/spree/core/spec/services/spree/carts/update_spec.rb
+++ b/spree/core/spec/services/spree/carts/update_spec.rb
@@ -88,6 +88,49 @@ module Spree
described_class.call(cart: cart, params: { preferred_stock_location_id: other_store_location.prefixed_id })
}.to raise_error(ActiveRecord::RecordNotFound)
end
+
+ it 'rebuilds delivery proposals when the pickup intent changes' do
+ expect(cart).to receive(:recalculate_for_address_change!)
+
+ described_class.call(cart: cart, params: { preferred_stock_location_id: pickup_location.prefixed_id })
+ end
+
+ it 'rebuilds delivery proposals when the pickup intent is cleared' do
+ cart.update!(preferred_stock_location_id: pickup_location.id)
+
+ expect(cart).to receive(:recalculate_for_address_change!)
+
+ described_class.call(cart: cart, params: { preferred_stock_location_id: '' })
+ end
+
+ it 'does not rebuild proposals when the same location is sent again' do
+ cart.update!(preferred_stock_location_id: pickup_location.id)
+
+ expect(cart).not_to receive(:recalculate_for_address_change!)
+
+ described_class.call(cart: cart, params: { preferred_stock_location_id: pickup_location.prefixed_id })
+ end
+
+ it 'builds collectable pickup proposals for a cart with no shipping address' do
+ create(:pickup_delivery_method, store: store)
+ # Ship-to-store counter: stock ships from the warehouse to the
+ # counter, so the warehouse-sourced package is collectable.
+ pickup_location.update!(pickup_stock_policy: 'any')
+ no_address_cart = create(:cart_with_line_items, customer: user, store: store, ship_address: nil)
+ # Pickup is only offered when every item's product type supports it.
+ pickup_capable_type = create(:product_type, fulfillment_types: %w[shipping pickup])
+ no_address_cart.line_items.each { |line_item| line_item.variant.product.update!(product_type: pickup_capable_type) }
+
+ result = described_class.call(
+ cart: no_address_cart,
+ params: { email: '[email protected]', preferred_stock_location_id: pickup_location.prefixed_id }
+ )
+
+ expect(result).to be_success
+ fulfillments = result.value.reload.fulfillments
+ expect(fulfillments).to be_present
+ expect(fulfillments.flat_map(&:delivery_rates).map(&:delivery_method)).to all(have_attributes(fulfillment_type: 'pickup'))
+ end
end
describe 'updating currency' do
diff --git a/spree/core/spec/services/spree/checkout/advance_spec.rb b/spree/core/spec/services/spree/checkout/advance_spec.rb
index 4400880205..797e7dec92 100644
--- a/spree/core/spec/services/spree/checkout/advance_spec.rb
+++ b/spree/core/spec/services/spree/checkout/advance_spec.rb
@@ -20,6 +20,20 @@ module Spree
expect(cart.reload.fulfillments).to be_present
end
+ it 'proposes fulfillments for a pure-pickup cart with no shipping address' do
+ pickup_location = create(:stock_location, pickup_enabled: true, pickup_stock_policy: 'any', store: store)
+ create(:pickup_delivery_method, store: store)
+ pickup_capable_type = create(:product_type, fulfillment_types: %w[shipping pickup])
+
+ cart.fulfillments.destroy_all
+ cart.update!(ship_address: nil, preferred_stock_location_id: pickup_location.id)
+ cart.line_items.each { |line_item| line_item.variant.product.update!(product_type: pickup_capable_type) }
+ cart.reload
+
+ expect(subject).to be_success
+ expect(cart.reload.fulfillments).to be_present
+ end
+
context 'with shipping method selection' do
let!(:other_method) do
create(:shipping_method).tap do |delivery_method|