markOrderPlaced
Mark an order placed: set order.placed_at and emit the order_placed event, once per order.
order_placed sends the order confirmation email and the order_placed webhook, and every subscriber that reacts to a new order listens to it. Core does not emit it when an order is created, because an order is created before it is paid. The payment method calls markOrderPlaced at its own moment of success:
- An offline method (Cash on Delivery, bank transfer): when the order is created.
- An online method (a card or wallet gateway): when the gateway accepts the payment, authorized or captured. PayPal also places an order whose capture is pending review.
Import
import { markOrderPlaced } from "@evershop/evershop/oms/services";
Syntax
markOrderPlaced(
orderId: number,
connection?: PoolClient
): Promise<OrderRow | null>
Parameters
| Parameter | Type | Description |
|---|---|---|
orderId | number | Order database ID (order_id, not the uuid) |
connection | PoolClient (optional) | The connection of your open transaction, started with startTransaction; any other connection is refused. The event then exists only if your transaction commits. Without it, markOrderPlaced runs in a transaction of its own. |
Inside your own transaction, always pass its connection. Without it, markOrderPlaced opens a second connection, which waits for the lock your transaction holds on the order row (taken by updatePaymentStatus, or by SELECT ... FOR UPDATE), while your transaction waits for the call. Both wait forever.
Return Value
Returns Promise<OrderRow | null>:
- the placed order, as it is after the update, when this call placed it;
nullwhen the order was already placed, or does not exist.
How it works
It runs one statement and emits only when that statement changes a row:
UPDATE "order" SET "placed_at" = NOW()
WHERE "order_id" = $1 AND "placed_at" IS NULL
RETURNING *
- Once per order. The first call sets
placed_at. Every later call finds it set and does nothing. Two calls at the same time are safe too: the second waits for the first transaction to finish, then findsplaced_atset. So a return page, a webhook and a cron job can all call it for the same order. - One payload. The event carries the full order row returned by the
UPDATE: the payment status your code set earlier in the same transaction, the order status andplaced_atincluded. - All or nothing.
placed_atand the event are written in the same transaction. If the event cannot be saved,placed_atis not set either, so the next call still sends it.
Examples
A webhook that records a captured payment. Update the payment status first, then mark the order placed, in the same transaction:
import {
markOrderPlaced,
updatePaymentStatus
} from "@evershop/evershop/oms/services";
await updatePaymentStatus(order.order_id, "my_payment_captured", connection);
await markOrderPlaced(order.order_id, connection);
await commit(connection);
An offline method marks its order placed after the order is created, from its bootstrap.ts:
import { error } from "@evershop/evershop/lib/log";
import { hookAfter } from "@evershop/evershop/lib/util/hookable";
import { markOrderPlaced } from "@evershop/evershop/oms/services";
hookAfter("createOrderFunc", async function (order) {
if (order.payment_method !== "bank_transfer") {
return;
}
try {
await markOrderPlaced(order.insertId);
} catch (e) {
// The order exists already: a failure here must not fail the checkout
error(e);
}
});
Notes
- Do not emit
order_placedyourself. Core emits it only throughmarkOrderPlaced. - An extension that still emits
order_placeddirectly keeps working: core does not emit it a second time. But its orders keepplaced_atempty. placed_atis empty while the payment is awaited, and for an order whose payment failed or was never made. Orders that existed before this column was added got their creation time, when their payment went further than pending, failed or canceled, when a payment was recorded for them, or when they are Cash on Delivery orders. Theirupdated_atis kept.- It does not change the payment status. Call updatePaymentStatus first.
See Also
- Payment Method Development — When to mark an order placed
- updatePaymentStatus — Update the payment status
- addPaymentTransaction — Record a payment transaction
- Events and Subscribers — React to
order_placed