Skip Navigation

[Resolved] Woocommerce Order status hook.

This support ticket is created 3 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 7 replies, has 2 voices.

Last updated by Puntorosso 3 years, 9 months ago.

Assisted by: Shane.

Author
Posts
#2012327

Hi, having a small blackout with this code

add_action( 'woocommerce_order_status_changed', 'func_status_change', 10, 4 );
function func_status_change ( $order_id, $old_status, $new_status, $order ) {
if ( $new_status == 'cancelled' || $new_status == 'failed' ){
update_post_meta( $order_id, 'wpcf-virtual-url' , $new_status );
}
}

#2012445

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Here are the private fields.

#2014251

Sorry for the delay. Still waiting for the customer permission to clone the site.
At the same time, while I did some more tests, I have stumbled to a really strange behaviour.

This code doesn't work (the "wpcf-woo-status" value don't change):

add_action( 'woocommerce_order_status_changed', 'func_status_change', 10, 4 );
function func_status_change ( $order_id, $old_status, $new_status, $order ) {
if ( $new_status == 'cancelled' || $new_status == 'failed' ){
update_post_meta( $order_id, 'wpcf-woo-status' , $new_status );
}
}

but this works:

add_action( 'woocommerce_order_status_changed', 'func_status_change', 10, 4 );
function func_status_change ( $order_id, $old_status, $new_status, $order ) {
if ( $new_status == 'cancelled' || $new_status == 'failed' ){
update_post_meta( $order_id, 'wpcf-woo-status' , $new_status );
die();
}
}

Adding a die() at the end, let the function change the "wpcf-woo-status" value.

Any idea why this happen?
Thanks

#2015273

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

Unfortunately i'm not sure what happens in this case.

However I've found a way to workaround this using the hook below.


function func_status_change($post_id) {
    // If this is a revision, get real post ID
    if ( get_post_type($post_id) == 'shop_order' ) 
        $woo_order = wc_get_order( $post_id );
  		$woo_order_status = $woo_order->get_status(); 
    if ( $woo_order_status == 'cancelled' || $woo_order_status == 'failed' ){
		update_post_meta( $post_id, 'wpcf-custom-order-status' , $woo_order_status );
		}	
     
    
}
add_action( 'save_post', 'func_status_change' );

I've tested this and it works fine and updates the status once the status is either failed or cancelled.

Please let me know if this alternative helps.
Thanks,
Shane

#2016395

Hi, thanks for the new code (please notice that he generate an error, and I had to add some missing {} )
Sadly, it doesn't solve my problem.
As my old code, it is properly fired, when the order status change, no question.
But it's the "update_post_meta" which is not working.
I simply cannot get the value saved on the Types custom field.

#2016419

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

I'm not able to debug the "woocommerce_order_status_changed" hook. It would appear that in the sequence of hooks that fire after the woocommerce_order_status_changed hook is making some unknown changes and causing the custom field not to update.

As you can see when the die function is added it breaks the hook at the point of the die and doesn't allow any other function after the die to run.

The best thing I can advise you to do regarding this hook is to get in touch with the Woocommerce support team and to as them for assistance with the hook.

I can only debug Toolset based hooks. Doesn't the code I provided fit your scenario of updating the post meta field if the status is set to Cancelled or failed?

Thanks,
Shane

#2016567

I think I have found the problem but not the solution.
The function works if inserted in the Toolset's settings "custom code", but not if inserted in my custom plugin.
Please notice that I have many other functions running without problem on my custom plugin.
And it doesn't work also if pasted in functions.php

Any idea? Can you try from your side? Thanks

#2016581

Found the solution!

add_action( 'save_post', 'func_status_change', 9999 );

Thanks a lot for your help