Class: Spree::DeliveryProfile

Inherits:
Object
  • Object
show all
Includes:
HasListPosition, SingleStoreResource
Defined in:
app/models/spree/delivery_profile.rb

Overview

The grouping that decides how a set of products ships: which origins can fulfill them, which destination zones apply, and which delivery methods are offered. Products reference a profile directly (nil = the store's default profile); carts split per profile, and a package's candidate methods are exactly its profile's methods.

Formerly Spree::ShippingCategory — the table is renamed in place, so 5.x category assignments become profile assignments (see docs/plans/6.0-delivery-profiles.md).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#products_count ⇒ Integer

How many products use this profile. Counted for every profile of a lazily preloaded list in one grouped query, since loading the products themselves to count them would fetch the whole catalog.

Returns:

  • (Integer)


131
132
133
134
135
136
137
138
# File 'app/models/spree/delivery_profile.rb', line 131

def products_count
  return @products_count if defined?(@products_count)

  profiles = lazy_preload_context&.records.presence || [self]
  counts = Spree::Product.where(delivery_profile_id: profiles.map(&:id)).group(:delivery_profile_id).count
  profiles.each { |profile| profile.products_count = counts.fetch(profile.id, 0) }
  @products_count
end

Class Method Details

.default_for(store) ⇒ Spree::DeliveryProfile?

The profile products fall back to when they reference none. Every store has exactly one.

Parameters:

Returns:



84
85
86
# File 'app/models/spree/delivery_profile.rb', line 84

def self.default_for(store)
  where(store: store, default: true).first
end

Instance Method Details

#accepts_provider?(_provider_class) ⇒ Boolean

Whether a delivery method using the given fulfillment provider may join this profile — the guard that keeps a Digital profile from quietly gaining a shipping method and reinterpreting its products.

Parameters:

  • _provider_class (Class) —

    a Spree::FulfillmentProvider subclass

Returns:

  • (Boolean)


46
47
48
# File 'app/models/spree/delivery_profile.rb', line 46

def accepts_provider?(_provider_class)
  true
end

#can_be_deleted? ⇒ Boolean

The default profile is load-bearing (products fall back to it); other profiles must be empty of products first.

Returns:

  • (Boolean)


90
91
92
# File 'app/models/spree/delivery_profile.rb', line 90

def can_be_deleted?
  !default? && products.none?
end

#covers_location?(stock_location) ⇒ Boolean

Whether packages originating from this stock location can carry the profile's products — true when any origin group covers it.

Parameters:

Returns:

  • (Boolean)


99
100
101
102
103
# File 'app/models/spree/delivery_profile.rb', line 99

def covers_location?(stock_location)
  return false if stock_location.nil?

  delivery_origin_groups.any? { |group| group.covers_location?(stock_location) }
end

#default_origin_group ⇒ Spree::DeliveryOriginGroup?

The group zones and methods join when none is named — the first by position, which for untouched profiles is the auto-created default.

Returns:



122
123
124
# File 'app/models/spree/delivery_profile.rb', line 122

def default_origin_group
  delivery_origin_groups.first
end

#digital? ⇒ Boolean

Whether this profile's products are delivered digitally. Declared by the kind, not derived from the method set — a freshly created profile with no methods yet must already know what it is.

Returns:

  • (Boolean)


53
54
55
# File 'app/models/spree/delivery_profile.rb', line 53

def digital?
  false
end

#fulfillable_stock_locations ⇒ ActiveRecord::Relation<Spree::StockLocation>

The locations this profile's products may be allocated from — the union of its origin groups' locations. Any group with no members widens the union to every store location.

Returns:



110
111
112
113
114
115
116
# File 'app/models/spree/delivery_profile.rb', line 110

def fulfillable_stock_locations
  groups = delivery_origin_groups.includes(:delivery_origin_group_locations)
  # Any group without members widens the union to every store location.
  return store.stock_locations if groups.empty? || groups.any? { |group| group.member_stock_location_ids.empty? }

  store.stock_locations.where(id: groups.flat_map(&:member_stock_location_ids).uniq)
end

#offers_pickup? ⇒ Boolean

Whether this profile's products can be collected at a merchant counter — true when any of its methods hands goods over via a pickup provider.

Returns:

  • (Boolean)


66
67
68
# File 'app/models/spree/delivery_profile.rb', line 66

def offers_pickup?
  delivery_methods.any?(&:pickup?)
end

#offers_shipping? ⇒ Boolean

Whether this profile's products ship to a customer address.

Returns:

  • (Boolean)


71
72
73
# File 'app/models/spree/delivery_profile.rb', line 71

def offers_shipping?
  delivery_methods.any?(&:requires_address?)
end

#requires_shipping_address? ⇒ Boolean

Whether checkout must collect a shipping address for this profile's products. Kinds override; the physical default derives from methods' providers (a pickup-only profile needs none).

Returns:

  • (Boolean)


60
61
62
# File 'app/models/spree/delivery_profile.rb', line 60

def requires_shipping_address?
  delivery_methods.any?(&:requires_address?)
end