Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 196 Vote(s) - 3.66 Average
  • 1
  • 2
  • 3
  • 4
  • 5
woocommerce_order_status_changed hook: getting old and new status?

#1
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?
Reply

#2
Try this code. According to me it should work based on your comments.

add_action( 'save_post', 'wpse63478_save' );
function wpse63478_save() {

if(!current_user_can('manage_options'))
return false;
if(!is_admin())
return false;
if($_REQUEST['post_type'] != 'shop_order')
return false;
if($_REQUEST['post_ID']!='')
{
$orderId = $_REQUEST['post_ID'];
$order = new WC_Order( $orderId );
$currentStatus = $order->status;
$requestedStautus = $_REQUEST['order_status'];
if ( $requestedStautus== 'on-hold' and $currentStatus == 'completed') {
//Do your work here
}
}
}
Reply

#3
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.
Reply

#4
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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through