Skip to main content
Recompute the commission lines of existing orders, for example after you change rates or when you change an order outside Mercur’s own workflows. Mercur already refreshes lines at checkout and on order edits, returns, claims, and exchanges.
1

Run the workflow

Pass the ids of the orders to refreshOrderCommissionLinesWorkflow. This route refreshes one order.
src/api/admin/orders/[id]/recompute-commission/route.ts
The workflow returns the lines it wrote for all the orders. It throws if any id doesn’t belong to an order.
2

Check the result

Read the lines back through query.graph, filtering by the order’s item ids.
Item lines carry item_id. Shipping lines carry shipping_method_id and the description Shipping Commission.

Backfill existing orders

The workflow takes any number of order ids and resolves them in one batch, so the rates load once per run. For a backfill, page through orders and refresh each page.
src/scripts/backfill-commission.ts
Each page is one workflow run. A refresh replaces lines by item and shipping method, so you can re-run the script safely.

Pass extra data with setCommissionContext

The setCommissionContext hook receives the calculation contexts before they reach the provider. Return a StepResponse with new contexts to fill additional_context for a custom provider, or return nothing to keep them.
src/workflows/hooks/commission-context.ts
Each context already has order_id, seller_id, currency_code, items, and shipping_methods. Items carry the product’s tags and attribute_value_ids, which the system provider ignores but your own provider can use.

React with commissionLinesUpserted

The commissionLinesUpserted hook runs after the lines are written, as part of the same workflow run. If your handler throws, the run fails, and at checkout that fails the cart completion too.
src/workflows/hooks/commission-written.ts
Use this hook to sync commission to an external ledger. Use the order events instead when your side effect shouldn’t be able to block the refresh.

Next steps