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.

12.6 KB
Raw Blame

Delivery Profiles

Status: Implemented (PR #14404, 2026-08-09) — Spree::DeliveryProfile is live as the renamed spree_shipping_categories table with STI kinds, Spree::ShippingCategory and Spree::ShippingMethodCategory are deleted, and spree:migrate_delivery_profiles is in the 5.6→6.0 manifest. 6.1: drop spree_shipping_method_categories and tighten the profile store_id constraint Target: Spree 6.0 Depends on: 6.0-fulfillment-and-delivery.md (shipped waves), 6.0-delivery-rate-provider.md (dynamic carrier rates), 6.0-delivery-zones.md, 6.0-product-types.md Author: Damian Legawiec + Claude (design sessions 2026-08-09) Last updated: 2026-08-09

Summary

Spree::DeliveryProfile is the store-scoped 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 falls back to the store's default profile); carts split per profile; a package's candidate methods are exactly its profile's methods filtered by origin and destination. The model is ShippingCategory promoted, not replaced: the 5.x table and the product foreign key are renamed in place, so historical data migrates by rename rather than by copy.

This supersedes three earlier decisions: "fulfillment types are NOT a model", "named delivery groups are 6.1-if-needed", and live-by-reference fulfillment_types on ProductType. Rationale for reversing inside the 6.0 window: there is no rewrite window in 6.1; custom string types were second-class (providers declare handled types, so a custom type could not use any carrier rate provider); the origin axis (stock locations ↔ methods) did not exist for shipping methods at all; and the array-on-ProductType was the sole live-by-reference exception to the product-type template doctrine.

The model

Spree::Store ─────────────────────────────── has many; exactly ONE default per store
 ▼
Spree::DeliveryProfile                    spree_delivery_profiles  (STI, prefix fp_)
 │   store_id · name · type · default · position   (renamed spree_shipping_categories)
 │   kinds: DeliveryProfiles::Shipping (default kind, physical goods)
 │          DeliveryProfiles::Digital  (digital? true, no shipping address,
 │                                         accepts only digital-provider methods)
 │          extension kinds via registers_subclasses_via
 │
 ├──< Spree::Product                         spree_products.delivery_profile_id
 │      (renamed shipping_category_id; REQUIRED — auto-assigned on create:
 │       type template, else the store's default profile)
 │      ProductType.delivery_profile_id = creation-time template
 │
 ├──< Spree::DeliveryOriginGroup             spree_delivery_origin_groups  (prefix og_)
 │      partitions the profile's fulfillment origins; every zone and method
 │      belongs to one group, so per-warehouse rate tables need no second
 │      profile. Auto-created nameless default with no members = every store
 │      location — single-group profiles never see the layer.
 │      group ↔ StockLocation join (spree_delivery_origin_group_locations;
 │      no rows ⇒ every store location)
 │
 ├──< Spree::DeliveryZone                    gains delivery_profile_id +
 │                                           delivery_origin_group_id
 │
 └──< Spree::DeliveryMethod
        delivery_profile_id     (required)
        delivery_origin_group_id (defaults: zone's group, else the profile's
                                 default group; zone and method must share it)
        delivery_zone_id        (nullable; same profile; nil = no destination
                                 restriction)
        NO modality column — behavior comes from classes:
          · FulfillmentProvider subclasses answer requires_address?,
            digital?, pickup? (Manual/Digital/Pickup/PickupPoint)
          · the profile kind validates composition (Digital profiles accept
            only digital-provider methods)
          · rate providers declare requires_address? (a carrier can only
            price methods that ship somewhere) instead of type lists
        ships-from narrowing · providers · calculator · markup · services ·
        rules — unchanged

No string vocabularies anywhere: the Spree.fulfillment_types registry, the ProductType array, AND the method fulfillment_type/modality column are all deleted. Every behavior question routes to a class: profile.digital?, profile.requires_shipping_address?, provider.requires_address?. The method form loses its type select — provider + profile say everything.

Runtime flow

  1. Split — Stock::Splitter::DeliveryProfile (replaces Splitter::FulfillmentType) partitions cart contents by each item's resolved profile.
  2. Allocate — the Coordinator allocates a profile's items only from stock locations the profile covers (its join; empty = all), for carts and for orders alike (OrderRouting::Strategy::Rules packs its ranked locations through the Coordinator). Among those it prefers an origin that can actually deliver to the destination: Stock::Prioritizer puts packages the Estimator finds a method for ahead of the rest, and an origin that cannot deliver gives up every unit the capable origins can supply between them, backorders included — ordering alone is not enough, because the Adjuster takes stock on hand anywhere over a backorder (decisions.md, 2026-10-26). Coverage stays the hard filter and deliverability the preference — a unit no capable origin can supply stays where it was allocated, so the cart can report it rather than silently losing the item, and checkout refuses to complete while any unit is in no fulfillment.
  3. Quote — for a package (profile, origin): candidates are the profile's methods whose ships-from set covers the origin, whose zone is nil or includes the ship address, passing the existing rule/calculator/audience filters; rate providers then fan out (multi-rate carriers untouched).
  4. Derive, never declare twice — Product#digital? ⇐ its profile kind (DeliveryProfiles::Digital); pickupable ⇐ the profile contains a pickup-provider method; the order requires an address ⇐ any item's profile answers requires_shipping_address? (kind-declared for Digital, derived from the methods' providers for physical kinds).

Key Decisions (do not deviate without discussion)

  • delivery_profile_id is required on Product (2026-08-09) — a product without one cannot be fulfilled, and nullable-with-fallback smears OR IS NULL across every profile join. Auto-assigned on create (type template, else store default), mirroring the 5.x category's presence+auto-assign behavior.
  • Profile on the Product, template on the ProductType (2026-08-09). delivery_profile_id lives on spree_products; ProductType's own delivery_profile_id is stamped onto products at creation only. This restores the uniform "creation-time template" doctrine that the fulfillment_types array violated, and makes a product's shipping behavior inspectable on the product itself.
  • Migration by rename, not by copy (2026-08-09). spree_shipping_categories → spree_delivery_profiles; spree_products.shipping_category_id → delivery_profile_id. 5.x products arrive assigned; the 5.x "Digital" category becomes the Digital profile. What the rename cannot express is handled by spree:migrate_delivery_profiles (in the 5.6→6.0 manifest): store assignment (5.x categories were global; products are single-owner since 5.5, so each profile goes to the store whose products use it, duplicated + remapped when shared — loudly logged); categories that do not narrow the store's method set fold into the store default profile (the schema migration creates "General" as each store's default); digital-only categories become Digital-kind profiles; and spree_shipping_method_categories (m:n) collapses into the method's single delivery_profile_id (solely-linked methods move in; multi-category methods stay in the default profile with a LOUD warning). The m:n table survives to 6.1 solely as that task's source.
  • No modality column, classes only (2026-08-09, supersedes the interim modality rename). The method's fulfillment_type column is dropped, not renamed; the Spree.fulfillment_types registry (engine config + accessors) is deleted outright — unreleased 6.0-dev surface. Delivery mechanics live on FulfillmentProvider class predicates (digital?, pickup?, pickup_point?, instance requires_address?); rate providers declare requires_address? instead of type lists; profile kinds are STI classes registered via Spree.delivery_profile_types. Wire formats may derive kind strings for display/filtering, but nothing stores or validates them.
  • ProductType#fulfillment_types array is removed (column + validation) in favor of the template profile reference. Capabilities derive from the profile's methods rather than being declared on the product side.
  • Zones belong to the profile; a method belongs to at most one zone. The spree_delivery_method_zones m:n join is dropped (backfilled to the first zone with a warning on multiples). A method with no zone has no destination restriction.
  • One default profile per store, enforced like StockLocation#default (ensure_one_default); undeletable; created by a Store after_create hook and by seeds/upgrade for existing stores. Non-default profiles are deletable only when no products reference them (model-level guard, per the RBAC axis-B convention).
  • Competitor names never appear in code or comments — grounding stays in plan documents.

Migration Path

  1. Structural migration (6.0, [8.1]): renames + new columns/joins as diagrammed; adds store_id/default/position to the renamed profiles table (nullable until the upgrade task assigns them; null: false tightening lands in 6.1); drops spree_delivery_method_zones after backfill.
  2. Spree::ShippingCategory + Spree::ShippingMethodCategory classes, deprecated associations (Product#shipping_category*, Variant#shipping_category*, DeliveryMethod#shipping_categories*), factories, permission-catalog subject, and typelizer entry are deleted. The permission catalog gains Spree::DeliveryProfile.
  3. spree:upgrade:migrate_delivery_profiles in the 5.6→6.0 manifest (resumable, anonymous-AR readers for the m:n) performs the judgement work listed above.
  4. 6.1: drop spree_shipping_method_categories, tighten profile store_id constraints.

Constraints on Current Work

  • New delivery features must resolve eligibility through the profile chain — never reintroduce parallel product-side capability arrays.
  • Behavior questions route through classes (provider_class.digital?, profile.requires_shipping_address?) — never reintroduce a stored type/modality string on methods, products, or profiles. Fulfillment.fulfillment_type (the historical column on fulfillments) remains as recorded data only.
  • The Store API surface for profiles is read-only derivation (product serializers may expose digital/pickupable); profile management is Admin API only.
  • Deliverability is asked once, through Stock::Estimator#deliverable? — allocation and the rate refresh share the method filter so they can never disagree about which origins can serve a destination. Never re-implement that question against zones or origin groups directly, and never answer it by calling a rate provider: allocation would then cost a carrier request per candidate warehouse.
  • Anything building packages sets Package#owner to the cart or order it is allocating. Left unset, #owner falls back to walking a unit's order_id, and cart-phase units carry none — the package would take its currency, store and destination from whatever order shares that id (which is what broke multi-currency freight quoting).

Open Questions

  • Whether ships-from narrowing needs promotion to named origin groups — pulled into 6.0 (2026-08-09, Damian): Spree::DeliveryOriginGroup ships now; per-method ships-from narrowing retires for shipping methods (the group is the origin axis) and DeliveryMethodStockLocation remains pickup-only (collection counters).
  • Multi-currency/multi-market interplay: profiles are store-scoped; market-aware profiles deferred to the channels/markets work.

References

  • Supersedes sections of 6.0-fulfillment-and-delivery.md ("Fulfillment types are a strict registered set", "Named delivery groups"), 6.0-product-types.md (fulfillment_types array), 6.0-delivery-zones.md (store-level zone ownership).
  • decisions.md 2026-08-09 entries (dynamic carrier rates; delivery profiles).
  • PR #14404.