Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Using wp_schedule_single_event to Change Woocommerce Order Status WordPress

  • REFUND REQUESTED

I need some help with the function below, I am not sure how to properly pass the order ID to the scheduled function so as to update the order status. I am still a newbie, so bare with me..

My ultimate goal is to create a scheduled event that will update woocommerce orders to 'complete' 2 days after they are created.

function schedule_order_status_update( $order_id ){

$order = new WC_Order( $order_id );

$args = array( $post->ID => $order );

wp_schedule_single_event( time()+172800, 'change_woo_order_status_hook', $args );

}

add_action('woocommerce_order_status_on-hold', 'schedule_order_status_update', 1, 1 );

add_action('change_woo_order_status_hook','change_woo_order_status');
function change_woo_order_status($order_id) {

$order = &new woocommerce_order( $order_id );

$order->update_status( 'complete', sprintf( __( 'Payment is past expiration time.', 'woocommerce' )) );

}

Answers (1)

2012-11-27

John Cotton answers:


function schedule_order_status_update( $order_id ){

// Just pass the id and then retrieve the order details (if you need them) in the scheduled function
$args = $order_id;


wp_schedule_single_event( time()+172800, 'change_woo_order_status_hook', $args );

}


Kyle comments:

Thank you for the reply,

I put your code in:

function schedule_order_status_update( $order_id ){


$args = $order_id;

wp_schedule_single_event( time()+172800, 'change_woo_order_status_hook', $args );

}

add_action('woocommerce_order_status_on-hold', 'schedule_order_status_update', 1, 1 );

add_action('change_woo_order_status_hook','change_woo_order_status');
function change_woo_order_status($order_id) {

$order = &new woocommerce_order( $order_id );

$order->update_status( 'complete', sprintf( __( 'Payment is past expiration time.', 'woocommerce' )) );

}


But am not quite sure how to check its success haha. I tried using wp_get_schedule( 'change_woo_order_status_hook' ); in a page template but there was no output. I created another event with the time set to 5 minutes, and am waiting, but not sure if that is too little time.


Kyle comments:

Okay I installed the 'Cron View' plugin and will post how the function works. Thanks again


John Cotton comments:

<blockquote> but not sure if that is too little time.</blockquote>

I didn't do the maths to see how the wait is...

What I tend to do with debugging these things is to do a wp_mail to myself with some state data.

Don't forget that the wp crons needs some activity on the site to trigger them, so you could be sitting waiting on a dev site for a long time!


Kyle comments:

Hmm, the function is still not firing correctly, it seems the plugin is also saying there are no arguments in the cron job..


Kyle comments:

I set the time to time()+300

Oh okay, I didn't know that about the activity thing.

I will try the wp_mail()


John Cotton comments:

<blockquote>I will try the wp_mail()</blockquote>
I'd stick one in both functions - you never know the initial function may never being called....


Kyle comments:

Okay, the $order_id is showing in the cron arguments, I had to pass it in the array:

add_action('woocommerce_order_status_on-hold', 'schedule_order_status_update', 1, 1 );
function schedule_order_status_update( $order_id ){
$args = array( 'post_id' => $order_id);
wp_schedule_single_event( time()+60, 'change_woo_order_status_hook', $args );
wp_mail('---','cron', $args);

}


But the cron fired and I think my function for using the ID is wrong. Do you know how I should properly pull the $args variable into the bottom function:

add_action('change_woo_order_status_hook','change_woo_order_status');
function change_woo_order_status($args) {

$order_id = $post->id;
$order = new WC_Order( $order_id );
$order->update_status( 'complete', sprintf( __( 'Payment is past expiration time.', 'woocommerce' )) );
wp_mail('---','cron', $args);

}


John Cotton comments:

Since you've put the arguments in an array (I didn't think you had too, but it's a while since I've used that function), you need to pull it out of the array:



$order_id = $args['post_id'];

$order = new WC_Order( $order_id );

$order->update_status( 'complete', sprintf( __( 'Payment is past expiration time.', 'woocommerce' )) );

wp_mail('---','cron', $args);


Kyle comments:

Okay the second function still is not firing for me. Including the wp_mail. The Cron Viewer is showing them as having been executed tho

I tried to make some changes


add_action('change_woo_order_status_hook','change_woo_order_status', 1, 1);
function change_woo_order_status($args) {
wp_mail('---','cron', $args['post_id']);
$order_id = $args['post_id'];
$order = &new WC_Order( $order_id );
$order->payment_complete();

}