I'm using woocommerce bookings.
I'm trying to trigger woocommerce order status to refund if the woocommerce_booking status is cancelled. I tried this code but it's not working.
global $woocommerce;
$order = new WC_Order( $order_id );
if ( 'cancelled' == $order->status ) {
   $order->update_status('refund', 'order_note');
}
To update order status on cancel status
add_action('woocommerce_cancelled_order','change_status_to_refund', 10, 1);
 function change_status_to_refund($order_id) {
    $order = new WC_Order( $order_id );
    $order->update_status('refund', 'order_note');
    exit;
 }
I hope it will help you. Thanks :)
add_action( 'woocommerce_order_status_changed', 'wc_order_status_changed', 99, 3 );
function wc_order_status_changed( $order_id, $old_status, $new_status ){
    if( $new_status == "cancelled" ||  $new_status == "refunded" ) {
        //code here.
    }
}
If you want use in some class action must be like this:
add_action( 'woocommerce_order_status_changed', array($this, 'wc_order_status_changed'), 99, 3 );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With