Skip to content
BonicBD
← All articles

The WooCommerce COD purchase event, done properly

By BonicBD Team · 2026-11-23 · Updated 2026-11-23 · 10 min read

WooCommerce is the easiest storefront to fire a confirmed COD purchase from, because the order status change happens on your own server. Here is the hook, the payload, and the three ways it goes wrong.

The WooCommerce COD purchase event, done properly

WooCommerce stores have an advantage that Shopify stores do not, and most of them never use it.

The order status change happens in your own PHP process. That means the moment an order is confirmed, you already have code running on a server you control, holding the order id and the value. You do not need a webhook from a platform, a sandboxed pixel, or a workaround. You need one hook.

This is how to wire it, and what breaks.

Why the thank-you page is the wrong source

On a prepaid store, the thank-you page load is a reasonable purchase signal. On a COD store it is a lie, for a reason that has nothing to do with WooCommerce.

Cash on delivery means the customer has requested an order, not bought anything. Somewhere between 30 and 45 percent of those requests never get delivered. A purchase fired at the thank-you page reports revenue from orders that will be cancelled on a phone call three days later.

Meta and TikTok then optimise toward the profile that fills a checkout form and cancels. You paid to find those people. The platforms learned the wrong lesson from your money.

So the goal is narrow: send the purchase when a human decides the order is going ahead.

The hook

WooCommerce fires woocommerce_order_status_changed with three arguments: the order id, the previous status, and the new status.

add_action('woocommerce_order_status_changed', function ($order_id, $from, $to) {
    // Only statuses that mean the sale is proceeding
    $confirm = ['processing', 'completed', 'shipped', 'delivered'];
    if (!in_array($to, $confirm, true)) {
        return;
    }

    $order = wc_get_order($order_id);
    if (!$order) {
        return;
    }

    wp_remote_post('https://track.yourdomain.com/c/<cid>/cod/webhook', [
        'timeout' => 8,
        'headers' => [
            'Content-Type'             => 'application/json',
            'x-bonicbd-webhook-secret' => '<from your panel>',
            'x-bbd-request-id'         => 'wc-' . $order_id . '-' . $to,
            'x-bbd-timestamp'          => (string) time(),
        ],
        'body' => wp_json_encode([
            'order_id'    => 'WC-' . $order_id,
            'type'        => 'order.status',
            'status'      => $to,
            'occurred_at' => time(),
        ]),
    ]);
}, 10, 3);

Three lines in that snippet are doing more work than they look like.

x-bbd-request-id is derived from the order and the status. If WooCommerce fires the hook twice, which it does on bulk updates and after certain plugin saves, the second call returns the first response instead of creating a second event. Randomise that string and you have built a duplicate generator.

x-bbd-timestamp is the current time. The endpoint rejects a request that falls outside its replay window, so a wrong clock on your server blocks the event. Check server time if events vanish.

x-bonicbd-webhook-secret goes in a header. A credential in a query string is refused outright, which is worth knowing before you spend an hour debugging a 400.

Decide what "confirmed" means on your store

WooCommerce's default statuses do not map cleanly onto reality in Bangladesh. A store might confirm by phone, then leave the order at processing for a week while it waits for a courier pickup.

BonicBD's default confirm list is broader than the four values in my snippet above:

confirmed, processing, completed, paid, shipped, order-shipped, dispatched, partially-shipped, delivered, delivered-cash-collected, out-for-delivery, ready-for-pickup, in-transit, intransit, picked-up.

And the list that closes an order without a purchase:

cancelled, canceled, failed, refunded, returned, trash.

Anything on neither list is ignored. That third behaviour is the important one. An unrecognised status is not treated as a sale, so a custom status from a plugin will do nothing rather than quietly teaching the ad platform a false positive.

If your store uses custom statuses, map them once per account. The mapping replaces the default lists rather than adding to them, so map both confirm and reject sides or you will lose behaviour you expected.

Where this goes wrong

I have seen the same three failures repeatedly.

The status fires too early. A COD store marks orders processing the moment the checkout completes, before anyone has called the customer. If processing is on your confirm list, you have rebuilt the thank-you-page mistake with extra steps. On a store like that, confirm should mean the phone call, not the placement.

Two plugins both send a purchase. A pixel plugin and a CAPI plugin and your new hook, all firing on the same order. The count triples. Fix it by enumerating every emitter before you add another one, not after.

The hook fires on a retry and nobody notices. WooCommerce can fire a status change more than once inside a single admin request. Without an idempotency id this creates duplicate events that look like a spike in sales.

There is a fourth, quieter one. A theme or plugin update overwrites functions.php and the hook disappears. Nothing fails visibly. The store still sells, the orders still confirm, and the ad platform just gets quieter. Put the hook in a small plugin, not in the theme.

Verifying it took

Four checks, in this order. Each one isolates a different layer, so a failure tells you where to look.

  1. Place a small test order. Confirm it through your normal process.
  2. Watch the order move from held to sent in the dashboard. If it stays held, the webhook is not reaching the endpoint.
  3. Open Meta Events Manager and look for exactly one purchase. Two with the same id means it merged, which is fine. Two with different ids means you have a second emitter.
  4. Compare the platform count against your WooCommerce order list for a week. That ratio is the only honest measure, and it should be close without being identical.

If step 2 works and step 3 shows nothing, the problem is downstream of collection: a wrong pixel id, an expired token, or a paused destination tag. The container log records what the destination answered, which is the evidence to read before changing anything.

The part that is not code

A hook takes an afternoon. Deciding which status genuinely means "this order is going ahead" takes a conversation with whoever runs your confirmation calls.

Get that decision written down before you write the array. Otherwise the code will work perfectly and count the wrong orders, and you will spend a month trusting a number that was never right.

Frequently asked questions

Does the purchase event fire twice when WooCommerce retries the status change?

Not with a stable request id. The snippet derives x-bbd-request-id from the order id and the status, so the second call returns the first stored response instead of creating a second event. Randomise that string and you have built a duplicate generator.

Which statuses should I put on the confirm list?

Ones that mean a human decided the order is going ahead. On a store where processing is set the moment checkout completes, that status is too early for a COD sale. Confirm should mean the phone call, not the placement.

Will a theme update break this?

Not if the hook lives in a small plugin. In functions.php it disappears with the next theme change, and nothing visibly fails.

Where does the secret go?

In the x-bonicbd-webhook-secret header. A credential in a query string is refused outright.

Do I need HPOS compatibility?

WooCommerce's newer order storage changes where order data lives, not whether the status hook fires. Both storages work, but check your other plugins before switching.


Practical next steps: COD order tracking · how the event path works · plans in BDT

Need help applying this article?

Compare the BonicBD feature set, review the setup guide, or contact the team if you want help with a live server-side GTM rollout.

Apply What You Just Read

Get a free tracking audit to see what your current setup is missing — or start a trial and test BonicBD on your own domain.