woocommerce_order_status_changed hook: getting old and new status?

Marcel picture Marcel · Sep 7, 2017 · Viewed 9.2k times · Source

How can I get the old status and new status of an order using the WooCommerce hook: woocommerce_order_status_changed?

This is my code, but only the $order_id is filled..

add_action('woocommerce_order_status_changed','woo_order_status_change_custom');
function woo_order_status_change_custom($order_id,$old_status,$new_status) {
//order ID is filled
//old_status and new_status never
//tested by logging the parameters
}

Now I can easily get the new status using this code:

 $order = new WC_Order( $order_id );
$orderstatus = $order->status;

But how can I get the previous order status, since $old_status is empty?

Answer

user1980232 picture user1980232 · Jan 29, 2018

I was looking for wc hooks and found this post. The reason the parameters are not set is that you're missing arguments in the add_action function. This function defaults to only one parameter. To have all three you should use:

add_action('woocommerce_order_status_changed', 'woo_order_status_change_custom', 10, 3);

The 10 is the default ordering for actions in Wordpress, and the last argument is the number of parameters that Wordpress should pass to the custom action.