The whole plugin is two hooks on WooCommerce. When an order is created it draws the next number and writes it onto the order. When WooCommerce shows an order's number anywhere, it shows the stored one. Two rules make this safe.
When an order is numbered
- WooCommerce creates an order — from the classic checkout, the block checkout, the Store API, the REST API or Add order in the admin — and fires
woocommerce_new_order. - If Give new orders sequential numbers is on and the order has no number yet, the plugin draws the next number from the counter.
- It writes two values onto the order: the raw number (
_gmson_number) and the number as it will be shown (_gmson_formatted). In the free edition the two are the same. Full edition: the shown number has your prefix, suffix and padding applied, with the date variables filled in at that moment. - The order is saved.
A draft order made by the block checkout while a customer is still typing is not numbered: WooCommerce does not fire woocommerce_new_order for drafts, only when the draft becomes a real order. So an abandoned block checkout uses up no number.
An order that already carries a number is never numbered again, however many times the hook fires.
With WooCommerce Subscriptions, the subscription itself is not numbered — Subscriptions creates it through the same order hook, and numbering it would leave a gap in your order sequence. Each renewal order draws a number of its own, and the plugin stops Subscriptions copying a number from the parent order onto the subscription, or from the subscription onto a renewal, so no two orders ever show the same number.
Rule 1 — the counter advances in one statement
The obvious way to number orders is to read the counter, add one and write it back. Two orders placed in the same instant both read 41, both write 42, and two orders share a number. In an invoice sequence, a duplicate is worse than a gap.
So the counter is moved in a single database statement. The counter is a row in WordPress's options table, gmson_counter. One UPDATE sets it to whichever is larger — the counter plus one, or your Start counting at number — and hands that value back on the same database connection, which the plugin then reads. Two requests drawing at once each get their own number, because the database itself serialises the update.
The same statement is what stops the start number moving the sequence backwards: it always takes the larger value.
Rule 2 — a number is given once and never changes
The shown number is stored on the order, not worked out each time it is displayed. If it were worked out on display, changing a prefix in February would quietly renumber every invoice since January.
So:
- Changing the start, prefix, suffix or padding only affects orders placed afterwards.
- A date variable records the date the number was assigned, not today's.
- Orders placed before you installed the plugin keep WooCommerce's own number. Nothing is backfilled.
- Turning Give new orders sequential numbers off stops numbering new orders; orders that have a number keep showing it.
Where the number appears
Everywhere WooCommerce asks an order for its number through $order->get_order_number(): the orders list, the order screen, customer and admin emails, the thank-you page, My Account, and anything that uses the same call, such as invoice and export plugins. The plugin filters woocommerce_order_number, the one place that call passes through.
The order's database ID does not change. Links, the REST API and WooCommerce's own internals still use it.
What "gap-free" means
Every number is drawn exactly once, in order, with none skipped by the plugin. A number still belongs to its order if that order later fails, is cancelled or is deleted — a deleted order leaves its number unused, because numbers are never handed out twice.
Free and full edition together
Both editions use the same counter option and the same two order meta keys. Install the full edition and the next order continues the sequence; its prefix and padding apply from that order on. Go back to the free edition and the same counter carries on, without the formatting.
Next: FAQ and troubleshooting.
