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.

Pin the arrangement a marketplace actually builds

Damian Legawiec867952630111
diff --git a/docs/plans/6.0-multi-vendor-marketplace.md b/docs/plans/6.0-multi-vendor-marketplace.md
index c08b73cc49..123a5de5a5 100644
--- a/docs/plans/6.0-multi-vendor-marketplace.md
+++ b/docs/plans/6.0-multi-vendor-marketplace.md
@@ -706,6 +706,8 @@ Even before this lands, work touching adjacent areas should:
- **Basic Stripe Connect execution → OSS (boundary amendment)** (RESOLVED 2026-07-15). Express onboarding + on-fulfillment transfers ship in the monorepo alongside the pulled-in Stripe gateway, for demo/adoption parity with the surveyed competitor — "money moves automatically" is the demo that sells, and transfers are inseparable from basic onboarding (a `Stripe::Transfer` needs a connected account). Enterprise repositions on money **operations**: automatic refund clawbacks (prorated transfer reversals + negative-balance netting), KYC/account-health workflows, ledger⇄Stripe reconciliation, payout reports incl. DAC7, facilitator taxes. Ledger reversal rows stay OSS in every mode — only the pullback execution is paid. Supersedes #13323's "execution stays Enterprise" framing (amendment by the issue author; update the issue). See `decisions.md` 2026-07-15.
- **`Spree::Vendor` name ownership → marketplace seller; inventory-operations' procurement model renamed `Spree::Supplier`** (RESOLVED 2026-07-14). See the Key Decisions entry and `decisions.md` 2026-07-14.
- **Commission is not a `Spree::Fee`** (RESOLVED 2026-07-14). `Fee` is buyer-facing and rolls into order totals; `CommissionLine` is an off-order platform↔vendor ledger. See Decision 2.
+- **Rule reference storage → arrays for sellers and categories, a table for products** (RESOLVED 2026-08-17). Follows the cardinality rule from `6.0-delivery-method-rules.md`, with one deliberate exception: `Promotion::Rules::Category` keeps its categories in a join table, and commission keeps them in a `category_ids` preference. The reason is what the array actually holds — matching walks *upward from the product* through `self_and_ancestors`, so a rule names only the categories an operator picked, never their descendants. A rate on "Electronics" is one id however deep the tree goes, and in practice nobody enumerates hundreds of categories or sellers into a single rule. Products stay in `spree_commission_rule_products` because a marketplace genuinely does name thousands.
+- **A parent category covers everything beneath it, to any depth** (RESOLVED 2026-08-17). Matching the promotion rules' behavior, and for the same reason: a marketplace that sets a rate on "Electronics" and later gets a "Electronics → Cameras → Mirrorless" leaf must not have that leaf silently fall through to the catch-all because the catalog was reorganised. Verified to four levels, with a sibling branch correctly not matching.
- **Retiring a rate retires its rules, and keeps them readable** (RESOLVED 2026-08-17, shipped). `CommissionRule` and `CommissionRuleProduct` are `acts_as_paranoid`, so a soft-deleted rate's conditions are retired with it rather than deleted — and an edit that replaces a rule keeps the one it superseded. There is no restore UI for a rate any more than for a price list; soft delete exists to keep the historical picture, and a commission line is explained by the rule that matched it, so "why was this charged" has to stay answerable after the rule stops applying. This is a deliberate departure — no other rule model in core is paranoid — justified by commission lines being settlement records rather than configuration. Two consequences to keep: both uniqueness indexes constrain **live rows only** (partial on PostgreSQL/SQLite, a stored generated column on MySQL/MariaDB, verified against both), and `CommissionRate#rules=` identifies an incoming row against the rule of that kind the rate already carries, since clients send the whole set without ids and a blind build would collide with the live rule it was meant to replace.
- **Commission rules → typed classes, not a polymorphic subject** (RESOLVED 2026-08-17, shipped; supersedes the original shape). A rule was `(subject_type, subject_id)`, which could only ever name a record: a price band, a channel, a promotional window were each a migration plus an edit to the matcher, and a third party could add nothing. It is now STI over `Spree.commission_rules`, matching `Spree::PriceRule` and `Spree::DeliveryMethodRule` — a rule carries its own configuration, declares it through `preference_schema`, and a new kind is a subclass and a registration. Matching semantics are unchanged: every rule must hold, and a rule naming several records means any of them, so the OR moved inside a rule and there is still no `match_policy`. Reference lists follow the house cardinality rule — vendors and categories as `normalize_id_preference` arrays, products in a join table — which also deleted the hand-rolled store check in the controller, since the preference writer scope-checks as it writes. The admin surface follows the delivery-method rules editor: `GET /commission_rates/rule_types` for discovery, one row per condition, a picker where the kind names records and its preference schema otherwise.
- **Commission rule matching → every rule must hold (AND), a rule naming several records means any of them (OR)** (RESOLVED 2026-07-14, restated 2026-08-17 for the typed shape). Expresses plain lists and the vendor × category pairings today's Enterprise sells, with no `match_policy` knob; same semantics as the leading surveyed OSS engine, but precedence stays the operator's own ordering rather than implicit specificity. See Decision 4.
diff --git a/spree/core/spec/services/spree/commissions/typical_setup_spec.rb b/spree/core/spec/services/spree/commissions/typical_setup_spec.rb
new file mode 100644
index 0000000000..c89e817232
--- /dev/null
+++ b/spree/core/spec/services/spree/commissions/typical_setup_spec.rb
@@ -0,0 +1,66 @@
+require 'spec_helper'
+
+# The arrangement a marketplace actually builds: a few rates targeting
+# specific products and categories, a deal or two for particular sellers, and
+# a global rate for everything else.
+#
+# Worth pinning as one spec rather than only in pieces, because it is the
+# claim the defaults make — that an operator who never touches ordering gets
+# the ladder they expect, purely from creating rates in the order they think
+# of them.
+RSpec.describe 'a typical commission setup' do
+ let(:store) { @default_store }
+ let(:vendor) { create(:vendor, :approved, store: store) }
+ let(:cameras) { create(:category, store: store) }
+ let(:order) { create(:order, store: store, currency: 'USD') }
+
+ let(:promoted) { create(:product, store: store, vendor: vendor).tap { |p| p.categories << cameras } }
+ let(:camera) { create(:product, store: store, vendor: vendor).tap { |p| p.categories << cameras } }
+ let(:other_goods) { create(:product, store: store, vendor: vendor) }
+ let(:first_party) { create(:product, store: store) }
+
+ before do
+ Spree::Seeds::CommissionRates.call
+ store.commission_rates.find_by(code: Spree::Seeds::CommissionRates::DEFAULT_CODE).
+ update!(enabled: true, value: 20)
+
+ seller_deal = create(:commission_rate, store: store, name: 'Acme deal', value: 12)
+ create(:commission_vendor_rule, commission_rate: seller_deal, vendors: [vendor])
+
+ category_rate = create(:commission_rate, store: store, name: 'Cameras', value: 8)
+ create(:commission_category_rule, commission_rate: category_rate, categories: [cameras])
+
+ promo = create(:commission_rate, store: store, name: 'Launch promo', value: 3)
+ create(:commission_product_rule, commission_rate: promo, products: [promoted])
+ end
+
+ def rate_for(product)
+ line = create(:line_item, order: order, variant: product.default_variant, price: 100)
+ Spree::Commissions::ResolveRate.call(
+ line_item: line, vendor: product.vendor, store: store, currency: 'USD'
+ ).value
+ end
+
+ # Nobody dragged anything: each rate was created after the ones it should
+ # beat, and lands above them.
+ it 'orders itself from most specific to least, without being told to' do
+ expect(store.commission_rates.ordered.pluck(:name)).
+ to eq(['Launch promo', 'Cameras', 'Acme deal', 'Marketplace default'])
+ end
+
+ it 'charges the product rate on the promoted product' do
+ expect(rate_for(promoted).value).to eq(3)
+ end
+
+ it 'charges the category rate on that seller other cameras' do
+ expect(rate_for(camera).value).to eq(8)
+ end
+
+ it 'charges the seller deal on their goods outside that category' do
+ expect(rate_for(other_goods).value).to eq(12)
+ end
+
+ it 'charges the marketplace rate on everything else' do
+ expect(rate_for(first_party).value).to eq(20)
+ end
+end