woocommerce_order_status_changed Hook [Guide + Code]

Order status changed hook for WooCommerce

Every time an order moves from one status to another, WooCommerce fires a hook you can attach to. Need an order note when an order is processed, or an alert when a completed order goes back to pending? You can handle both without editing templates or polling the database.

That’s what woocommerce_order_status_changed is for. This guide covers its four arguments, where WooCommerce fires it, and working PHP examples you can adapt.

What Is woocommerce_order_status_changed?

It is a WooCommerce action that runs each time an order’s status changes, such as from pending to processing. It passes four arguments, so the callback must be registered with an accepted-argument count of 4. Otherwise the extra arguments never reach it.

Function Signature and Arguments

Register the hook with add_action(), and set the priority and the argument count:

<?php
add_action( 'woocommerce_order_status_changed', 'my_callback', 10, 4 );

The callback receives these arguments in order:

  • $order_id is the order ID as an integer.
  • $from_status is the previous status key without the wc- prefix, such as pending.
  • $to_status is the new status key, such as processing.
  • $order is the WC_Order object, so you can read or update the order without loading it again.

When WooCommerce Fires the Hook

WooCommerce fires this hook from the order’s status transition logic, after the status-specific hooks run. It only fires when the order already had a status, so it does not run for a new order. The table compares it with the two related hooks.

HookFires whenArguments
woocommerce_order_status_{status}The order enters a status, such as completed$order_id, $order
woocommerce_order_status_{from}_to_{to}One specific change, such as pending_to_processing$order_id, $order
woocommerce_order_status_changedAny status change$order_id, $from_status, $to_status, $order

Basic Example: Add an Order Note

The simplest use is to log each change as an order note. add_order_note() saves the note right away, so you do not need to call save().

<?php
add_action( 'woocommerce_order_status_changed', 'my_log_status_change', 10, 4 );

function my_log_status_change( $order_id, $from_status, $to_status, $order ) {
    $order->add_order_note(
        sprintf( 'Status changed from %s to %s.', $from_status, $to_status )
    );
}
Order notes panel showing a status change note

Practical Examples

Once you have the order ID, you can load any other order data. For the full loading process, see the wc_get_order() guide. The examples below use the four arguments directly.

Alert When a Completed Order Goes Back to Pending

Reopening a completed order can point to a refund or fulfillment problem, so it is worth an email to the store admin. The check compares both sides of the transition.

<?php
add_action( 'woocommerce_order_status_changed', 'my_alert_reopened_order', 10, 4 );

function my_alert_reopened_order( $order_id, $from_status, $to_status, $order ) {
    if ( 'completed' !== $from_status || 'pending' !== $to_status ) {
        return;
    }

    wp_mail(
        get_option( 'admin_email' ),
        sprintf( 'Order #%d was reopened', $order_id ),
        sprintf( 'Order #%d moved from completed to pending. Total: %s', $order_id, $order->get_total() )
    );
}

Count Status Changes per Order

Orders that bounce between statuses often need a closer look. This example stores the number of changes in order meta. The key starts with an underscore, so it stays out of the custom fields box in the admin.

<?php
add_action( 'woocommerce_order_status_changed', 'my_count_status_changes', 10, 4 );

function my_count_status_changes( $order_id, $from_status, $to_status, $order ) {
    $count = (int) $order->get_meta( '_my_status_change_count' );

    $order->update_meta_data( '_my_status_change_count', $count + 1 );
    $order->save();
}

save() is required here, because update_meta_data() only changes the object in memory.

Common Mistakes

  • Registering with the default argument count. With the default of 1, WordPress passes only $order_id, and the other parameters are undefined.
  • Looping on your own status change. Calling $order->update_status() inside this hook fires it again. Check $to_status before you change anything.
  • Expecting it on new orders. A new order has no previous status, so this hook does not fire on creation. Use woocommerce_new_order for that.
  • Reading post meta directly. With HPOS, orders are not stored in wp_posts. Use $order->get_meta() and $order->update_meta_data(). See the HPOS guide for details.

FAQs About woocommerce_order_status_changed

Does woocommerce_order_status_changed Fire for New Orders?

Not normally. The hook needs a previous status, and a new order has none. Use woocommerce_new_order to run code when an order is created.

When Should I Use a Status-Specific Hook?

Use woocommerce_order_status_{status} when your code only cares about one status, such as completed. It is shorter and easier to read than a check inside the general hook. For payment-specific logic, see the woocommerce_payment_complete guide.

Does It Work With HPOS?

Yes. The hook passes the order object, so you can use the same methods with legacy post storage and with High-Performance Order Storage. Avoid get_post_meta() and update_post_meta() on order IDs, because they bypass HPOS.

Conclusion

woocommerce_order_status_changed gives you one place to react to every status change. Register it with four arguments, use the order object for reads and writes, and guard against loops when your code changes status.

Need custom order workflows for your store? Hire a WooCommerce developer, or reach out to me directly.

About The Author

Picture of Andriy Haydash

Andriy Haydash

Andriy Haydash is a WordPress Expert who helps people build and launch successful WordPress membership and e-learning websites.

Note: Not all of the articles are written directly by me.
Affiliate Disclaimer: Some links in the post may be my affiliate links

Kinsta - Managed Hosting for WordPress

Before YOU Leave...
Join My Newsletter

Get practical tips & tricks on how to start, grow and market your course/membership site.