5

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');
}
7
  • What is not working? Did yoy check $order->status? Jan 9, 2017 at 17:41
  • yes I checked order status. It remains the same. It should change to refund but it's not working.
    – PPrevoo
    Jan 9, 2017 at 18:07
  • spreek jij toevallig Nederlands Jeroen? Ik zit al weken met hetzelfde probleem. Komt het omdat de order wordt geplaatst via een booking? (woocommerce bookings)
    – PPrevoo
    Jan 9, 2017 at 18:09
  • Yes, i am living in the Netherlands. I am not a real expert but looked on Google and found this SO question and this article suggesting your code should work. You could test this by changing to an other status. The docs indicate that refund is only working if your "payment gateway supports it". Try manually first and update your question with the found results. Jan 9, 2017 at 18:28
  • seems nobody understands my question. I need woocommerce booking status (cancelled) to changes woocommerce order status to refund.
    – PPrevoo
    Jan 9, 2017 at 19:53

6 Answers 6

6

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 :)

2
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 );
0

You need to fetch the status of your order and then check your required condition and update it accordingly.

$order_status = $order->get_status();
0

I know this is an old post, but i have just make it on my latest wordpress/woocommerce install

add_action('woocommerce_booking_cancelled', 'my_booking_cancelled_handler', 10, 1);
function my_booking_cancelled_handler ( $booking_id ) {
  $booking = new WC_Booking( $booking_id );
  $order_id = $booking->get_order_id();
  // check order for your business logic
  // refund or not ;-) it's up to you
}

I hope this helps someone.

0
//Try this


add_action( 'woocommerce_order_status_changed', 'auto_destroy_failed_orders', 10, 4 );

function auto_destroy_failed_orders( $order_id, $old_status, $new_status, $order )
 {
    if ( ( $new_status == 'completed' )) {
        // Order status complete 
        $order->update_status( 'custom-status' );

    }else if ( ( $new_status == 'failed' )) {
        $order->update_status( 'custom-status' );

    }else if ( ( $new_status == 'b2c-shipment' )) {
        // custome status
        $order->update_status( 'custom-status' );

    }else if ( ( $new_status == 'on-hold' )) {
        // Order status to on-hold
        $order->update_status( 'custom-status' );

    }else if ( ( $new_status == 'refunded' )) {
        // Order status to refunded
        $order->update_status( 'custom-status' );

    }else if ( ( $new_status == 'failed' )) {
        // Order status to failed
        $order->update_status( 'custom-status' );

    }else if ( ( $new_status == 'processing' ) ) {
        // Order status to processing
        $order->update_status( 'custom-status' );
    } 
}
-1

Hey you can try this hook!!

https://therichpost.com/change-product-order-status-woocommerce-hook

Hope this will help you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.