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.7 KB
Raw Blame

Product Market Availability (Territory Exclusions)

Status: Draft — design finalized 2026-09-07, nothing built Target: Spree 6.1 (purely additive — nullable column + exception rows; pulls forward if a 6.0 slot opens) Depends on: Markets (shipped), Channels/ProductPublication (shipped), Products::ForContext (shipped), Channel-Markets allowlist (6.0-channel-markets.md — adjacent axis, in progress) Author: Damian + Claude Last updated: 2026-09-07 Tracking: Linear V-3618 · GitHub spree/spree#14613

Summary

A store selling one range into many markets sometimes may not sell a handful of products in particular territories — exclusive distribution agreements, licensing, regulation. Today the only expression is a channel per territory, which duplicates surfaces to hide two products. This plan adds a product-level market availability axis: a nullable market_availability mode (NULL=all markets | all_except | only) plus exception rows naming markets, composed with publications as an intersection on the read path — published on the channel ∧ available in the market — with full hiding in excluded markets (listings, search, collections, feeds, direct URL) and a completion validation covering the mid-cart market change. The design copies the behavior of the only platform that runs channel and market as two axes on one product (full hide, everything-by-default, one publishing surface) while avoiding its storage tax (per-market membership materialization) and keeping the doctrine intact: Channel answers surface, Market answers territory, Catalog answers buyer — this is the territory answer, deliberately not a market catalog (the door 2026-08-28 closed stays closed).

Key Decisions (do not deviate without discussion)

  • Exceptions-only storage, zero backfill (2026-09-07, interactive review): spree_products.market_availability is nullable with NULL meaning "all markets" — every existing product is correct at migration time with nothing written; a restricted product flips the mode and gains join rows only for the named markets (the 100-products × 20-markets × 2-exceptions case is 3 rows). New markets need nothing: all_except includes them, only excludes them, correct by construction. (The no-defaults-on-string-columns rule is about workflow-set statuses; NULL-as-all is the deliberate fallthrough here, read as market_availability || 'all'.)
  • Full hide, not a purchase-block warning (2026-09-07): an excluded market never sees the product — a "not available in your market" PDP advertises exactly the product the distributor holds rights to. The market-competent platform made the same call (redirect/null; no visible-but-unsellable state per market). A visible-but-blocked display mode is a recorded possible later addition, not v1.
  • Composition, not a second visibility mechanism: availability = published(channel) AND available(market), both evaluated per request (Spree::Current.channel / Spree::Current.market), nothing derived or stored — nothing to de-sync. Merchant comprehension is handled the way the two-axis platform does it: one Availability card on the product page showing publications and market availability together, both defaulting to "everywhere" so neither axis exists until deliberately narrowed.
  • Checkout re-validates on market change. No platform enforces territory at checkout (their restriction resolves at browse context) — but Spree's market can shift mid-cart via the address, so Carts::Complete (and the address-driven market change path) validates line items against the new market with a clear removal/error code. A correctness improvement over every precedent.
  • Deny-list first-class: all_except is the primary mode (sell globally, subtract territories — the exclusive-distribution shape, which only one platform's UX offers and none model natively); only covers licensing ("approved for AU+NZ only") with the same rows.

Design Details

# spree_products.market_availability  string, nullable (NULL = all)
# spree_product_market_availabilities:
#   product_id  bigint, null: false
#   market_id   bigint, null: false
# unique index (product_id, market_id); partial index on
# spree_products.market_availability WHERE NOT NULL (restricted set is tiny)

class Spree::Product
  MARKET_AVAILABILITY_MODES = %w[all_except only].freeze # nil = all

  has_many :market_availability_entries,
           class_name: 'Spree::ProductMarketAvailability', dependent: :destroy
  has_many :restricted_markets, through: :market_availability_entries, source: :market

  def available_in_market?(market)
    case market_availability
    when nil then true
    when 'all_except' then !restricted_market_ids.include?(market&.id)
    when 'only' then restricted_market_ids.include?(market&.id)
    end
  end
end
  • Read path: one scope (Product.available_in_market(market)) applied in the same base the channel publication filter runs in — store products listing, Products::ForContext, search indexing/filtering (the search provider indexes the exception data so the database and Meilisearch providers filter alike), collections, sitemap/feeds. Product show in an excluded market → not found (the storefront decides redirect vs 404, matching its own conventions; the API answer is 404 in that market context). SQL stays cheap: the IS NULL short-circuit covers virtually every row; EXISTS touches only flagged products.
  • Cart/checkout: Carts::AddItem/UpsertItems validate the variant's product against the cart's market; Carts::Complete re-validates (market may have changed via address) with code: 'not_available_in_market'; completed orders untouched (history is history).
  • Admin: the product page's Availability card gains a Markets section (mode select + market multi-select via the standard combobox), beside the existing channel publishing; the three new attributes ride the product write paths; bulk via the spreadsheet editor later if asked.
  • Serializers: admin product exposes mode + markets; store API simply never returns the product in an excluded market — no flag to leak.

Migration Path

  1. Schema + model + scope + spec battery (mode semantics, new-market behavior, guest/no-market context = available).
  2. Read-path wiring (listing, ForContext, search providers, show, feeds) + cart/completion validations.
  3. Admin API + dashboard Availability card + SDK types.

Nothing to migrate.

Constraints on Current Work

  • Availability = publication ∧ market availability, evaluated on read — never denormalize an "effective visibility" flag anywhere.
  • Never model territory availability as a catalog (assignment or otherwise) — catalogs answer buyers; the 2026-08-28 market-catalog closure stands.
  • New market-context read surfaces (feeds, recommendations, APIs) must apply the scope — a surface that skips it leaks restricted products.

Open Questions

  1. Visible-but-unpurchasable display mode (per-product opt-in) — deferred; no territory precedent exists and full hide is the safe default. Unblocked by: a merchant needing the softer mode.

References

  • Merchant feedback, 2026-09 (exclusive distribution; three modes; full hiding incl. direct URL; territory ≠ buyer).
  • Platform research 2026-09-07 (two-axis precedent, full-hide choice, deny-list UX, storage tax) — private, not in this repo.
  • decisions.md 2026-08-28 (market-catalog closure), 2026-08-30 (one question per entity), 2026-09-07 (this decision).
  • docs/plans/6.0-channel-markets.md — the adjacent channel↔market allowlist (which surface serves which territories; orthogonal to which products a territory may buy).