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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Added `equires_address?`, `requires_address?` and `serves_location?` to Fulfillment Providers
Damian Legawiec45fe2437026b
diff --git a/spree/core/app/models/spree/delivery_method.rb b/spree/core/app/models/spree/delivery_method.rbindex baabf8a0f4..25333da12c 100644--- a/spree/core/app/models/spree/delivery_method.rb+++ b/spree/core/app/models/spree/delivery_method.rb@@ -80,8 +80,10 @@ module Spreeendend+ # Zones describe the customer's destination address, so only methods+ # that deliver to one are zone-checked.def requires_zone_check?- !digital? && !pickup?+ requires_address?enddef build_tracking_url(tracking)@@ -118,8 +120,18 @@ module Spreefulfillment_type == 'pickup_point'end+ # Whether this method delivers to a customer shipping address —+ # answered by the fulfillment provider (digital and the pickup kinds+ # are address-free; custom providers default to requiring one).def requires_address?- !digital? && !pickup?+ provider.requires_address?+ end++ # Whether this method can serve a package sourced from the given stock+ # location — answered by the fulfillment provider (shipping/digital are+ # location-agnostic; merchant pickup requires an eligible counter).+ def serves_location?(stock_location)+ provider.serves_location?(self, stock_location)end# The FulfillmentProvider strategy handling this method's fulfillments.diff --git a/spree/core/app/models/spree/fulfillment_provider/base.rb b/spree/core/app/models/spree/fulfillment_provider/base.rbindex 8408410847..8dd7b59984 100644--- a/spree/core/app/models/spree/fulfillment_provider/base.rb+++ b/spree/core/app/models/spree/fulfillment_provider/base.rb@@ -5,6 +5,22 @@ module Spree# time; registered via Spree.fulfillment_providers. Replaces the legacy# ShipmentHandler name-constantize mechanism.class Base+ class << self+ # Fulfillment types this provider can handle, so admin UIs can offer+ # only the providers that fit the chosen type. An empty list means+ # "any type".+ #+ # @return [Array<String>]+ def fulfillment_types+ []+ end++ # @return [String] human-readable name for admin UIs+ def provider_name+ name.demodulize.titleize+ end+ end+# @return [Boolean] whether the fulfillment may transition to fulfilleddef can_fulfill?(_fulfillment)true@@ -16,6 +32,21 @@ module Spreefalseend+ # Whether this provider dispatches to a customer shipping address.+ # Address-free providers (digital, the pickup kinds) override to false;+ # it also drives delivery-zone checks, which describe the customer+ # destination.+ def requires_address?+ true+ end++ # Whether the method can serve a package sourced from the given stock+ # location. Shipping-like providers dispatch from anywhere; merchant+ # pickup overrides this to require an eligible pickup location.+ def serves_location?(_delivery_method, _stock_location)+ true+ end+# Performs the provider-side dispatch when a fulfillment fulfills.## @return [Hash] optionally { tracking_number:, tracking_url: }diff --git a/spree/core/app/models/spree/fulfillment_provider/digital.rb b/spree/core/app/models/spree/fulfillment_provider/digital.rbindex 148deef3ce..90518406c7 100644--- a/spree/core/app/models/spree/fulfillment_provider/digital.rb+++ b/spree/core/app/models/spree/fulfillment_provider/digital.rb@@ -5,10 +5,18 @@ module Spree# legacy non-idempotent checkout callback that created one link per line# item regardless of quantity.class Digital < Base+ def self.fulfillment_types+ ['digital']+ end+def auto_fulfill?trueend+ def requires_address?+ false+ end+def create_fulfillment(fulfillment)fulfillment.line_items.each do |line_item|ensure_links_for(line_item)diff --git a/spree/core/app/models/spree/fulfillment_provider/pickup.rb b/spree/core/app/models/spree/fulfillment_provider/pickup.rbindex 9571e326f2..f7abe83bbb 100644--- a/spree/core/app/models/spree/fulfillment_provider/pickup.rb+++ b/spree/core/app/models/spree/fulfillment_provider/pickup.rb@@ -3,6 +3,29 @@ module Spree# Merchant-location pickup: hand-over is confirmed by staff, no# provider-side mechanics.class Pickup < Base+ def self.fulfillment_types+ ['pickup']+ end++ def requires_address?+ false+ end++ # A pickup rate is only real when the package can reach a counter the+ # customer may collect from: either the package is sourced from an+ # eligible pickup location itself, or some eligible location accepts+ # remote stock (pickup_stock_policy 'any' — ship-to-store transfers).+ def serves_location?(delivery_method, stock_location)+ return false if stock_location.nil?++ stock_locations = Spree::StockLocation.arel_table+ delivery_method.available_pickup_locations.where(+ stock_locations[:id].eq(stock_location.id).or(+ stock_locations[:pickup_stock_policy].eq('any')+ )+ ).exists?+ end+def create_fulfillment(_fulfillment){}enddiff --git a/spree/core/app/models/spree/fulfillment_provider/pickup_point.rb b/spree/core/app/models/spree/fulfillment_provider/pickup_point.rbindex 5e86827e49..83dde7ee6b 100644--- a/spree/core/app/models/spree/fulfillment_provider/pickup_point.rb+++ b/spree/core/app/models/spree/fulfillment_provider/pickup_point.rb@@ -4,6 +4,14 @@ module Spree# discovery and validation live on the delivery method's# PickupPointProvider; hand-over mechanics default to manual.class PickupPoint < Base+ def self.fulfillment_types+ ['pickup_point']+ end++ def requires_address?+ false+ end+def create_fulfillment(_fulfillment){}enddiff --git a/spree/core/app/models/spree/stock/estimator.rb b/spree/core/app/models/spree/stock/estimator.rbindex 74e4d98c2e..89ae49e6be 100644--- a/spree/core/app/models/spree/stock/estimator.rb+++ b/spree/core/app/models/spree/stock/estimator.rb@@ -68,6 +68,7 @@ module Spreeship_method.available_to_display?(display_filter) &&ship_method.include?(order.ship_address) &&+ ship_method.serves_location?(package.stock_location) &&ship_method.eligible_for_package?(package) &&calculator.available?(package) &&(calculator.preferences[:currency].blank? ||diff --git a/spree/core/lib/spree/testing_support/factories/delivery_method_factory.rb b/spree/core/lib/spree/testing_support/factories/delivery_method_factory.rbindex 2128f8e4f6..26b583f7fb 100644--- a/spree/core/lib/spree/testing_support/factories/delivery_method_factory.rb+++ b/spree/core/lib/spree/testing_support/factories/delivery_method_factory.rb@@ -20,5 +20,21 @@ FactoryBot.define dofulfillment_provider { 'Spree::FulfillmentProvider::Digital' }association(:calculator, factory: :digital_shipping_calculator, strategy: :build)end++ factory :pickup_delivery_method, class: Spree::DeliveryMethod do+ name { 'Store pickup' }+ code { 'PICKUP' }+ fulfillment_type { 'pickup' }+ fulfillment_provider { 'Spree::FulfillmentProvider::Pickup' }+ association(:calculator, factory: :shipping_no_amount_calculator, strategy: :build)+ end++ factory :pickup_point_delivery_method, class: Spree::DeliveryMethod do+ name { 'Parcel locker' }+ code { 'PICKUP_POINT' }+ fulfillment_type { 'pickup_point' }+ fulfillment_provider { 'Spree::FulfillmentProvider::PickupPoint' }+ association(:calculator, factory: :shipping_no_amount_calculator, strategy: :build)+ endendenddiff --git a/spree/core/spec/models/spree/delivery_method_spec.rb b/spree/core/spec/models/spree/delivery_method_spec.rbindex 34d6ba48e4..3f0e919fc9 100644--- a/spree/core/spec/models/spree/delivery_method_spec.rb+++ b/spree/core/spec/models/spree/delivery_method_spec.rb@@ -35,14 +35,75 @@ describe Spree::DeliveryMethod, type: :model doendend- describe '#requires_zone_check?' do- it 'returns true if the shipping method is not digital' do- expect(delivery_method.requires_zone_check?).to be_truthy+ describe '#requires_address? / #requires_zone_check?' do+ it 'is true for shipping methods' do+ expect(delivery_method.requires_address?).to be true+ expect(delivery_method.requires_zone_check?).to be trueend- it 'returns false if the shipping method is digital' do- delivery_method = create(:digital_delivery_method)- expect(delivery_method.requires_zone_check?).to be_falsey+ it 'is decided by the fulfillment provider, not the fulfillment type' do+ [+ ['digital', 'Spree::FulfillmentProvider::Digital'],+ ['pickup', 'Spree::FulfillmentProvider::Pickup'],+ ['pickup_point', 'Spree::FulfillmentProvider::PickupPoint']+ ].each do |fulfillment_type, provider|+ method = create(:delivery_method, fulfillment_type: fulfillment_type, fulfillment_provider: provider)++ expect(method.requires_address?).to be false+ expect(method.requires_zone_check?).to be false+ end+ end++ it 'is true for the Manual provider whatever the fulfillment type' do+ method = create(:delivery_method, fulfillment_type: 'pickup')++ expect(method.fulfillment_provider).to eq('Spree::FulfillmentProvider::Manual')+ expect(method.requires_address?).to be true+ end+ end++ describe '#serves_location?' do+ let(:warehouse) { create(:stock_location) }+ let(:counter) { create(:stock_location, pickup_enabled: true) }++ it 'is true for shipping methods regardless of location' do+ expect(delivery_method.serves_location?(warehouse)).to be true+ end++ context 'for a pickup method' do+ let(:pickup_method) { create(:pickup_delivery_method) }++ it 'accepts a package sourced from a pickup-enabled location' do+ expect(pickup_method.serves_location?(counter)).to be true+ end++ it 'rejects a package sourced from a plain warehouse' do+ expect(pickup_method.serves_location?(warehouse)).to be false+ end++ it 'rejects a nil location' do+ expect(pickup_method.serves_location?(nil)).to be false+ end++ it 'respects the configured pickup-location set' do+ other_counter = create(:stock_location, pickup_enabled: true)+ pickup_method.pickup_locations << other_counter++ expect(pickup_method.serves_location?(counter)).to be false+ expect(pickup_method.serves_location?(other_counter)).to be true+ end++ it 'accepts any source when an eligible counter takes remote stock (ship-to-store)' do+ counter.update!(pickup_stock_policy: 'any')++ expect(pickup_method.serves_location?(warehouse)).to be true+ end++ it 'ignores inactive counters' do+ counter.update!(active: false)++ expect(pickup_method.serves_location?(counter)).to be false+ endendend