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.

7.8 KB
Raw Blame

Merchant-Composed Order Stages

Status: Draft — design finalized 2026-08-30, nothing built Target: Spree 6.1 Depends on: Service Workflows (6.0-service-workflows.md — statuses doctrine), B2B Wholesale Shipping (6.0-b2b-wholesale-shipping.md — the orders this serves), Events system Author: Damian + Claude Last updated: 2026-08-30 Tracking: Linear V-3617 · GitHub spree/spree#14612

Summary

A B2B order can be commercially active for weeks while nothing sits in a warehouse: confirmed → in production → quality control → ready for shipment. Today that lifecycle has no home — order status is a frozen three-value constant (draft | placed | canceled) written only by workflows, and rightly so. This plan gives merchants a parallel, per-store stage axis they compose visually in the dashboard: ordered stage definitions, a current-stage pointer on the order, per-stage customer visibility, an order-level expected ready date, and events on every stage change for automation and mail. No commerce platform offers this (2026-08-30 research): the closest is merchant-created status labels pinned to fixed states that never participate in the workflow, or fully custom state machines that are code-config only — while a paid-app economy on the biggest platform proves the demand. The design is the two-axis shape the codebase already uses (fulfillment status vs tracking_status) and the one the ERP world converged on: stages are process tracking and presentation; semantic statuses stay code-owned.

Key Decisions (do not deviate without discussion)

  • Stages are data; statuses stay code (2026-08-30, interactive review): Spree::OrderStage rows are store-scoped, ordered (acts_as_list → drag-reorder per dashboard convention), named, translatable, with a customer_visible flag. The order gains a nullable order_stage_id. Semantic statuses (Order::STATUSES, payment/fulfillment rollups) remain code-defined and workflow-written — a merchant must never be able to break checkout, money, or stock from a settings screen. Order is deliberately NOT migrated to has_status for this; the stage axis is the answer to extensibility, and add_status remains the code-level path for models that have it.
  • No transition graph, ever. Any stage can follow any stage; staff move orders freely (Spree::Orders::SetStage — a plain service, the single writer, publishing order.stage_changed). Validating transitions centrally would be a state machine by another name — the exact thing 6.0 removed. Automation (mails, webhooks, external systems) rides the event.
  • Auto-enter bindings are event-driven labeling, never gating (2026-08-30, follow-up discussion): a stage may optionally declare auto_enter_on — an event name picked from a curated dropdown backed by the Spree.order_stage_events registry (order.placed, the payment schedule's deposit-covered / balance-paid moments, fulfillment.fulfilled, fulfillment.delivered, …; extensions register their own events, which is how a developer's custom status feeds a merchant's stage). A subscriber reacts to the bound event by calling SetStage — the binding only ever moves the pointer after something already happened; it can veto nothing. One stage per event per store (validated). This is what makes the composer a flow, not a label list — and it stays doctrine-safe because reaction is not enforcement. A merchant can compose either presentation: true stages beside the derived payment/fulfillment axes, or one linear timeline where those milestones appear as bound stages.
  • Stages carry zero commerce semantics. Nothing in checkout, payments, stock, fulfillment, tax, or pricing may read the stage. The one designed touchpoint is display composition: the customer order view shows the stage (when visible) beside the payment schedule and fulfillment state.
  • expected_ready_on is an order-level merchant promise (nullable date) — distinct by design from the variant's preorder_ships_at (a supply fact) and the fulfillment's estimated_delivery_at (carrier truth). Set by staff, shown to the customer, never derived.
  • Deleting a stage definition nils the pointer (dependent: :nullify
    • a dashboard confirm listing affected order counts); order history keeps the trail through the emitted events, not through the row.

Design Details

class Spree::OrderStage < Spree.base_class
  include Spree::SingleStoreResource
  include Spree::Metadata

  acts_as_list scope: :store_id

  belongs_to :store, class_name: 'Spree::Store'
  has_many :orders, class_name: 'Spree::Order', dependent: :nullify

  validates :name, presence: true, uniqueness: { scope: spree_base_uniqueness_scope }
  # customer_visible boolean, default false
  # auto_enter_on    string, nullable — an event name from Spree.order_stage_events;
  #                  one stage per event per store (validated)
end
  • spree_orders.order_stage_id (nullable — nil is "no stage", today's behavior) + spree_orders.expected_ready_on (date, nullable).
  • Orders::SetStage.call(order:, stage:, actor:) — staff surfaces only; publishes order.stage_changed with old/new stage in the payload.
  • Admin API: /admin/order_stages CRUD (settings scope, reorder via position), order_stage_id + expected_ready_on on order update; ransackable order_stage_id for list filtering.
  • Store API: order serializer exposes stage (name only, when the stage is customer_visible) and expected_ready_on — composing the buyer view the merchant sketched: stage + expected date + payment schedule + shipment summary + documents. Because stages are ordered and visibility-flagged, the serializer can also expose the visible subset's position ("step 3 of 6") for a storefront progress indicator.
  • A stage-binding subscriber listens to registered events and calls SetStage; Spree.order_stage_events is the curated registry the composer's dropdown reads (core lifecycle events seeded; extensions append).
  • Dashboard: a Settings → Order stages composer (drag to order, visibility toggle per stage — the visual composition that is the differentiator), a stage select on the order page, a list column + filter.

Migration Path

  1. Schema + model + SetStage + event.
  2. Admin API + dashboard composer + order-page select.
  3. Store serializer + storefront display.

Nothing to migrate; no stages defined = no change anywhere.

Constraints on Current Work

  • Nothing in commerce logic may branch on order_stage_id — stages are tracking/presentation; a feature needing a real gate needs a real status or workflow validation.
  • No transition rules on stages — not in core, not via extension points; automation belongs on the order.stage_changed event.
  • Don't add per-store status vocabularies to has_status models — statuses stay process-global code; per-store process expression is this stage axis.

Open Questions

  1. Per-channel or per-flow stage sets (retail orders shouldn't show a "Production" stage) — deferred; v1 is one set per store and the customer-visible flag covers most of it. Unblocked by: merchant feedback after 6.1.

References

  • Merchant feedback, 2026-08 (configurable pre-shipment stages; production lead time; customer visibility) + the maintainer's status-composer question.
  • Landscape research 2026-08-30 (no platform offers composed stages; label-on-state and code-config precedents; app-economy demand) — private, not in this repo.
  • Two-axis precedent: spree/core/app/models/spree/fulfillment.rb (status vs tracking_status).
  • Statuses doctrine: spree/core/app/models/concerns/spree/has_status.rb ("validating transitions centrally would be a state machine by another name").