Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

woocommerce_order_status_changed hook: getting old and new status?

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?

like image 274
Marcel Avatar asked Sep 07 '17 07:09

Marcel


2 Answers

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.

like image 119
user1980232 Avatar answered Nov 17 '22 11:11

user1980232


Because you didn't added NUMBER of parameters at the end of add_action call. Here is correct line:

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

"10, 3" means "I want 3 parameters will be sent to my callback function". By default there is only 1 parameter (order_id) will be sent.

like image 3
Epsiloncool Avatar answered Nov 17 '22 11:11

Epsiloncool