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?
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.