/spree:spree-order-totalsfrom spree
Explains how Spree 6 order totals are built from typed money rows (TaxLine, Discount, Fee), the totals columns, recalculation workflow, and custom adjusters. Use when totals don't add up, taxes seem wrong, or adding fees/discounts.
Ships in spree. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
A cart's or order's total is assembled from typed money rows — there is no polymorphic Spree::Adjustment. Each kind of money has its own table, its own denormalized sum on the purchase, and a single workflow that keeps them consistent.
Coming from 5.x adjustments? See
spree-upgrade-5-to-6(spree:migrate_adjustments_to_typed_rows).
total = item_total + delivery_total + adjustment_total
adjustment_total = (all discount rows) + fee_total + additional_tax_total
| Column (Cart & Order) | What it holds |
|---|---|
item_total | Σ line item amount (price × quantity), before discounts/tax |
delivery_total | Σ fulfillment cost (the selected delivery rates) |
discount_total | Σ promotion discount rows (negative) — manual discounts are not in it (see Gotchas) |
fee_total | Σ fee rows (always ≥ 0), duties included |
additional_tax_total | Σ tax lines with included: false — added on top |
included_tax_total | Σ tax lines with included: true — already inside prices; informational |
adjustment_total | discounts + fees + additional tax |
total | What the customer pays |
payment_total | Captured payments minus refunds |
tax_total (method) | included_tax_total + additional_tax_total |
amount_due (method) | max(total − payment_total − applied store credit, 0) |
Line items and fulfillments carry their own per-adjustable columns (discount_total, included_tax_total, additional_tax_total, adjustment_total, taxable_adjustment_total = discounts, non_taxable_adjustment_total = fees, pre_tax_amount) so per-line math and returns don't need to re-query rows.
Placed marketplace orders also carry commission_amount_total / commission_tax_total / commission_total — charged to the seller, never part of total (see spree-marketplace).
All three include Spree::TypedAdjustmentLine: dual cart_id / order_id owner (exactly one — read row.owner), label, amount (+ display_amount), metadata.
| Model (prefix) | Attaches to (exactly one…) | Kinds | Sign | Written by |
|---|---|---|---|---|
Spree::TaxLine (tl_) | line item, fulfillment, or fee | — (included true/false, taxability_reason) | ≥ 0 | The tax provider only (see spree-taxes) |
Spree::Discount (disc_) | line item or fulfillment | promotion, manual | ≤ 0 | Promotion adjuster; admin (manual) |
Spree::Fee (fee_) | line item, fulfillment, or neither (order-level) | surcharge, handling, gift_wrap, cod, payment, duty (extensible) | ≥ 0 | Adjusters, integrations, admin |
code, value, value_type (promotion FKs nullify on delete); a tax line keeps rate, label, provider_id, jurisdiction; a duty fee keeps its HS code / origin / rate in metadata. Never re-derive a placed order's money from today's catalog.fee_id); duty fees are excluded from the taxable set.Spree::Carts::RecalculateTotals — the single seamEverything that changes money ends in this workflow (Spree.cart_recalculate_totals_workflow; order twin Spree.order_recalculate_totals_workflow = Spree::Orders::RecalculateTotals). cart.recalculate_totals! is the shortcut.
reset caches → total_quantity → item_total / delivery_total / payment_total
→ [unless money-frozen] regenerate rows:
Spree.adjusters.each { |a| a.adjust(cart) } # promotions, your fees/discounts
run_hooks :set_tax_line_context # carts.recalculate_totals.set_tax_line_context
cart.tax_provider.estimate(cart, **cart.tax_estimate_inputs, context:)
→ fold rows into per-line and purchase columns → total → one update_columns
Two-pass by design: adjusters persist the discounted base first, then tax is estimated on it.
What triggers it — writes, not steps. Item add/upsert/remove (via Spree::Carts::Recalculate, which also rebuilds delivery proposals and re-activates promotions), address or market change (recalculate_for_address_change! also re-prices items), delivery-rate selection, coupon/gift card/store credit apply/remove, payments, cart merge, and once more inside the completion lock — the charged total is never trusted from an earlier request. Reading a cart doesn't recalculate (except the Store API GET backfilling missing delivery proposals).
Every Store API write returns the full cart with fresh totals; storefronts never call a "recalculate" endpoint.
Spree::Adjusters::Promotion evaluates every eligible promotion on every recalculation and persists only the winner per competition group: line-level actions compete per line item, fulfillment actions per fulfillment, order-level actions order-wide (winner distributed across lines). Most negative amount wins; ties go to the newest action. Losing candidates simply aren't written, so they "reinstate" automatically when they become best. It also deletes any kind: 'promotion' row it didn't write this pass. Details: spree-promotions.
For a placed order (completed?) — or when called with resum_only: true (seller-split child drafts) — the workflow skips row regeneration: no adjusters, no promotion competition, no tax estimation. It only re-sums the rows the order already has. Consequences:
| Change | Service | Admin API |
|---|---|---|
| Manual discount (flat/percent, one line or distributed) | Spree::Orders::Discounts::Create/Update/Destroy | POST/PATCH/DELETE /api/v3/admin/orders/:id/discounts |
| Fee | Spree::Orders::Fees::Create/Update/Destroy | /api/v3/admin/orders/:id/fees |
| Items / attributes | Spree::Orders::Update (items: [...]), Orders::UpdateItem, Orders::AddItem | PATCH /api/v3/admin/orders/:id, /api/v3/admin/orders/:id/items |
| Tax lines | — (provider-written) | GET /api/v3/admin/orders/:id/tax_lines (read-only) |
Spree::Orders::Discounts::Create.call(order: order, label: 'Price match', value: '15', value_type: 'flat')
Spree::Orders::Fees::Create.call(order: order, attributes: { label: 'Handling', kind: 'handling', amount: 2.5 })
Each of these services re-sums and then calls order.update_statuses!, because no event announces a moved total. A paid order that gains a fee reads partially_paid, and one cut below what was paid reads overcharged (amount_due floors at zero, so only the status says a refund is owed). A custom service that edits a placed order's money must make the same call. Orders::Update is all-or-nothing: if any item change in the request is refused, no line, quantity or address change is saved.
Only manual discounts are editable — touching a promotion row fails (422 via the API). Because tax is not re-estimated on a placed order, a manual discount does not reduce already-charged tax; handle tax corrections through your tax provider / a return.
An adjuster owns a family of rows and is re-run on every (unfrozen) recalculation. Contract: subclass Spree::Adjusters::Base, implement idempotent update (write the rows that should exist, delete the ones that shouldn't). order is the cart (or draft order); order.fees / order.discounts attach to the right owner automatically.
# server/app/models/my_app/adjusters/gift_wrap.rb
module MyApp
module Adjusters
class GiftWrap < Spree::Adjusters::Base
PRICE = BigDecimal('4.99')
def update
wrapped = order.line_items.select { |li| li.metadata['gift_wrap'].present? }
order.fees.where(kind: 'gift_wrap').where.not(line_item_id: wrapped.map(&:id)).delete_all
wrapped.each do |li|
order.fees.find_or_initialize_by(kind: 'gift_wrap', line_item_id: li.id)
.update!(label: 'Gift wrapping', amount: PRICE * li.quantity)
end
end
end
end
end
# server/config/initializers/spree.rb — MUST be after_initialize:
# core assigns Spree.adjusters = [Spree::Adjusters::Promotion] in its own after_initialize,
# which would wipe anything appended earlier.
Rails.application.config.after_initialize do
Spree.adjusters << MyApp::Adjusters::GiftWrap
Spree::Fee::KINDS << 'insurance' unless Spree::Fee::KINDS.include?('insurance') # custom fee kind (validated, not frozen)
end
Custom discounts from an adjuster must use kind: 'manual' (Spree::Discount::KINDS is frozen to promotion/manual) and attach to a line item or fulfillment. Don't write kind: 'promotion' rows — the promotion adjuster deletes rows it didn't create.
Fees written by an adjuster are taxed in the same pass (the provider runs after adjusters).
A customs duty is a Spree::Fee with kind: 'duty', written by a landed-cost integration (typically an adjuster). Rules:
purchase.taxable_items excludes duties) — import VAT, if any, is written by the duty provider itself as a tax line against the duty fee.metadata; never recompute a placed order's duty.hs_code, country_of_origin (manufacture, not ship-from), customs_description.Money is a string ("135.60") plus a pre-formatted display_* twin ("$135.60"). Render display_*; if you must do math, use a decimal library or integer cents — never JS floats.
cart.display_item_total // items
cart.display_discount_total // negative
cart.display_delivery_total
cart.display_fee_total // cart.fees[] for line-by-line (label, kind, display_amount)
cart.display_tax_total // safe single tax line (included + additional)
cart.display_total
cart.display_amount_due // after gift cards / store credit
additional_tax_total only — never included_tax_total (it's inside the prices already). The #1 European-storefront bug.total, don't add them again. Show duty fees under their own label.cart.discounts / order.discounts is a per-applied-promotion summary (name, code, amount, display_amount, promotion_id), not the typed Spree::Discount rows; typed rows are on the Admin API (adminClient.orders.discounts).amount_due, not total.null.discount_total counts only promotion rows. Manual discounts (and custom adjuster discounts) are in adjustment_total and total but not in discount_total. If your summary shows discount_total + total, a manual discount looks like a missing amount — show item_total + delivery_total + fee_total + additional_tax_total − total as "Discounts", or list Spree::Discount rows.update_column(:total, …)) — the next recalculation overwrites them. Write rows, then call recalculate_totals! / the order services.TaxLine rows by hand. They're replace-all per item by the provider; yours vanish on the next estimate.Spree::Carts::Recalculate) — re-apply after edits. A plain totals recalculation (delivery, discounts, tax moving) keeps the gift card and resizes its payment to the new total instead.row.order during checkout — use row.owner.node_modules/@spree/docs/dist/developer/core-concepts/order-totals.md, discounts.md, fees.mdnode_modules/@spree/docs/dist/developer/how-to/custom-promotion.md — custom adjustersspree-taxes, spree-promotions, spree-checkout, spree-returns, spree-pricingspree/core/app/workflows/spree/carts/recalculate_totals.rb, spree/core/app/models/spree/adjusters/, spree/core/app/models/spree/{tax_line,discount,fee}.rbWhat SKILL.md sets; muted values are Claude Code's defaults
39 skills in this plugin
/spree-api-v3Use when the user is calling or debugging Spree 6's REST API v3 — the Store API (storefronts, mobile apps, customers), the Admin API (integrations, back-office tools) or the Seller API (marketplace sellers). Covers auth (publishable/secret keys, JWT audiences), scopes and permissions, prefixed IDs, the cart → order split, checkout `requirements[]`, order statuses, the `{data, meta}` envelope, expand/fields, Ransack filters, errors, idempotency, localization headers and rate limits. Common phrasings include "Spree API", "Store API", "Admin API", "publishable key", "secret key", "X-Spree-Api-Key", "X-Spree-Token", "API scopes", "403 required_scope", "cart_ vs or_", "complete cart", "cart 404 after checkout", "prefixed IDs", "expand", "Idempotency-Key", "429 rate limit", "v3 endpoint", "Admin API OpenAPI spec". For ADDING or documenting (OpenAPI) a new endpoint use spree-resource; for AI agents driving the store over MCP use spree-mcp; for roles/permissions internals use spree-auth-permissions./spree-auth-permissionsUse when the user is working on who can sign in to Spree 6 and what they may do — staff roles and permission keys, secret-key scopes, registering permission scopes for an extension, 403 errors with `required_permission` / `required_scope`, storefront ownership rules (`Spree::Storefront::AccessPolicy`), custom login strategies (SSO, OIDC, Auth0/Okta/Entra/Firebase), JWT audiences, or swapping the customer/admin user class. Common phrasings include "Spree roles", "staff permissions", "add a role", "read_orders permission", "Spree.permissions.register_scope", "API key scopes", "403 Missing permission", "who can see this order", "SSO for the dashboard", "OIDC login", "custom authentication strategy", "Spree.customer_class", "admin_user_class", "CanCanCan in Spree 6". Spree 6./spree-b2bUse when building B2B or wholesale commerce on Spree 6 — companies and buyer organizations (company trees, divisions, members, invitations, buying on a company's behalf), catalogs and negotiated pricing (owned price lists, assignment to companies or customer groups), customer groups, gated storefronts (`storefront_access` login_required / prices_hidden), the Next.js wholesale portal, minimum order quantity / order multiples / order minimums, PO numbers, orders on account (pay by invoice, staff-keyed draft orders, `payment_pending`) and freight shipping (cartons, pallets, unpriced freight rates). Common phrasings include "B2B", "wholesale", "build a B2B store", "pay by invoice", "net terms", "draft order for a company", "company account", "Spree::Company", "division", "negotiated prices", "customer-specific pricing", "price list per company", "MOQ", "order multiple", "case pack", "order minimum", "PO number", "login to see prices", "wholesale portal", "X-Spree-Channel", "freight", "pallet"./spree-catalogUse when the user is working with the Spree 6 product catalog — products, variants, option types/values, the default variant, product types (templates), categories vs collections, custom fields, media (images, video, media library), channel publications, search providers (database, spree_meilisearch, custom), digital assets, or catalog imports/exports. Common phrasings include "create a product", "add variants", "size and color options", "product type", "category tree", "collection", "brand", "custom field on products", "product images", "product video", "primary_media", "product not showing in store", "publish product on channel", "Meilisearch", "reindex", "custom search provider", "digital download", "master variant". Covers the catalog graph and how to read/write it from Ruby, the Admin SDK and `spree api`./spree-checkoutUse when the user is working on Spree 6 checkout — the Cart → Order split, checkout requirements and advisory steps, `Spree::Checkout::Registry` (add_requirement / register_step / base_steps), cart completion (`Spree::Carts::Complete`), `carts.complete` / `carts.add_item` workflow hooks, addresses, payment sessions, guest checkout, cart association/merge on login, or the Store API / `@spree/sdk` checkout sequence. Common phrasings include "checkout broken", "cart won't complete", "why can't this cart complete", "add a checkout step", "require a field before checkout", "require terms acceptance", "skip the delivery step", "order stuck", "double charge", "guest checkout", "cart not found after checkout", "merge carts on login", "custom checkout validation", "block checkout if…". Covers where each customization belongs and how completion guarantees idempotency./spree-cliUse when calling the Spree 6 Admin API from the command line or driving it programmatically as an agent — exploring endpoints, reading or mutating store data, and especially DEBUGGING (inspecting an order/product/customer, checking why a request failed, reproducing a 403/422). The `spree api` command group in `@spree/cli` is a `gh api`-style generic HTTP client: `spree api get|post|patch|delete <path>` plus offline discovery (`spree api endpoints`, `spree api schema`). Common phrasings include "spree api", "spree CLI", "call the admin API from the terminal", "spree api get", "inspect this order", "why is this Spree request failing", "list admin endpoints", "spree auth". Also covers the project-side dev commands agents reach for while debugging (`spree shell`, `spree test`, `spree console`, `spree add`, `spree plugin new`). For SDK/TypeScript integration use spree-typescript-sdk; for raw API protocol details use spree-api-v3.npx claudepluginhub spree/agent-skills --plugin spreeOther plugins' skills for the same job