WooCommerce order notes from a warehouse webhook
Order notes are the timeline of a WooCommerce order: status changes, payment messages, and anything a plugin adds with add_order_note(). A note can be private (only the shop staff sees it) or a customer note, which WooCommerce also sends to the customer by email. That makes them a natural place for messages from an external fulfilment service.
Problem: A warehouse sends a webhook when an order is picked, packed and dispatched. The shop staff wants to see these steps in the order, and the customer should get one email with the tracking number when the parcel leaves, not three.
Solution: Register a REST route that verifies an HMAC signature of the raw request body, ignores repeated deliveries of the same event, adds private notes for the internal steps and a customer note with the tracking link for the dispatch, stores the tracking number as order meta and completes the order.
The endpoint
add_action( 'rest_api_init', function (): void {
register_rest_route( 'ha-fulfilment/v1', '/events', array(
'methods' => 'POST',
'callback' => 'ha_fulfilment_event',
'permission_callback' => 'ha_fulfilment_verify_signature',
) );
} );
function ha_fulfilment_verify_signature( WP_REST_Request $request ): bool {
$secret = (string) get_option( 'ha_fulfilment_secret' );
$signature = (string) $request->get_header( 'x-signature' );
if ( '' === $secret || '' === $signature ) {
return false;
}
$expected = hash_hmac( 'sha256', $request->get_body(), $secret );
return hash_equals( $expected, $signature );
}
The signature is computed over the raw body with a shared secret, so nobody can forge an event without the secret, and a changed body invalidates the signature. A plain secret in a header would be sent along with every request and is worth less if it leaks once. hash_equals() compares in constant time. The exact format (hex or base64, header name) comes from the documentation of the warehouse service.
The events
function ha_fulfilment_event( WP_REST_Request $request ): WP_REST_Response {
$data = $request->get_json_params();
$event_id = sanitize_text_field( $data['event_id'] ?? '' );
$type = sanitize_key( $data['type'] ?? '' );
$order = wc_get_order( absint( $data['order_id'] ?? 0 ) );
if ( ! $order ) {
return new WP_REST_Response( array( 'error' => 'unknown order' ), 404 );
}
// Webhooks are retried: handle each event only once.
$seen = (array) $order->get_meta( '_ha_fulfilment_events' );
if ( '' !== $event_id && in_array( $event_id, $seen, true ) ) {
return new WP_REST_Response( array( 'status' => 'duplicate' ), 200 );
}
switch ( $type ) {
case 'picked':
$order->add_order_note( __( 'Warehouse: items picked.', 'textdomain' ) );
break;
case 'packed':
$order->add_order_note( __( 'Warehouse: parcel packed.', 'textdomain' ) );
break;
case 'dispatched':
$tracking = sanitize_text_field( $data['tracking_number'] ?? '' );
$carrier = sanitize_text_field( $data['carrier'] ?? '' );
$url = esc_url_raw( $data['tracking_url'] ?? '' );
$order->update_meta_data( '_ha_tracking_number', $tracking );
$order->add_order_note(
sprintf(
/* translators: 1: carrier, 2: tracking number, 3: tracking URL */
__( 'Your order is on its way with %1$s. Tracking number: %2$s %3$s', 'textdomain' ),
$carrier,
$tracking,
$url
),
true // customer note: shown in My Account and sent by email
);
$order->update_status( 'completed', __( 'Dispatched by the warehouse.', 'textdomain' ) );
break;
default:
return new WP_REST_Response( array( 'error' => 'unknown event' ), 400 );
}
if ( '' !== $event_id ) {
$seen[] = $event_id;
$order->update_meta_data( '_ha_fulfilment_events', array_slice( $seen, -20 ) );
}
$order->save();
return new WP_REST_Response( array( 'status' => 'ok' ), 200 );
}
Only the dispatch note has true as second argument. WooCommerce then sends the “Customer note” email, shows the note in the customer’s account and marks it as a note to the customer in the admin. The internal notes stay private. The note text is plain text in the email; WooCommerce turns URLs in it into links.
update_status() saves the order itself and sends the “Completed order” email. If the warehouse’s dispatch email is enough, skip the customer note or disable that email, so the customer does not get two messages at once.
The meta and the notes are written through the order object (update_meta_data(), save()), which works with both the classic post storage and High-Performance Order Storage: migrate to HPOS.
Read the notes back
$notes = wc_get_order_notes( array(
'order_id' => $order->get_id(),
'type' => 'customer', // 'internal' for private notes, '' for all
) );
foreach ( $notes as $note ) {
echo esc_html( $note->date_created->date_i18n( 'Y-m-d H:i' ) . ' ' . $note->content ) . "\n";
}
Notes are stored as comments of the type order_note; wc_get_order_notes() is the API to read them, a direct query on wp_comments is not needed.
NOTE: Answer the webhook quickly. If the handling gets heavy (generating documents, calling another API), store the event and process it with Action Scheduler, and return 200 at once; otherwise the warehouse runs into its timeout and sends the event again. Hooks for the order status changes themselves: WooCommerce order status transitions.