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');
}
Grégory C
  • 419
  • 2
  • 7
  • 23
PPrevoo
  • 51
  • 3
  • What is not working? Did yoy check $order->status? – Jeroen Heier Jan 09 '17 at 17:41
  • yes I checked order status. It remains the same. It should change to refund but it's not working. – PPrevoo Jan 09 '17 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 09 '17 at 18:09
  • Yes, i am living in the Netherlands. I am not a real expert but looked on Google and found [this](http://stackoverflow.com/questions/22935358/woocommerce-change-order-status-with-php-code) SO question and [this](https://stanhub.com/how-to-change-order-status-in-woocommerce/) article suggesting your code should work. You could test this by changing to an other status. The [docs](https://docs.woocommerce.com/document/woocommerce-refunds/) indicate that refund is only working if your "payment gateway supports it". Try manually first and update your question with the found results. – Jeroen Heier Jan 09 '17 at 18:28
  • seems nobody understands my question. I need woocommerce booking status (cancelled) to changes woocommerce order status to refund. – PPrevoo Jan 09 '17 at 19:53
  • If anyone's still struggling with this, here's a helpful guide: https://wpza.net/woocommerce/change-order-status-to-complete-for-woocommerce-bookings/ – WPZA Dec 19 '18 at 20:50
  • I think you should check this: https://docs.woocommerce.com/document/woocommerce-refunds/#section-1. Then take apart what woocommerce is doing behind these actions and just call that PHP yourself. – Bret Weinraub Mar 23 '19 at 15:45

6 Answers6

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

dineshkashera
  • 1,442
  • 1
  • 15
  • 26
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 );
Rostiel
  • 31
  • 6
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();
Ivar
  • 6,138
  • 12
  • 49
  • 61
MakeWebBetter
  • 463
  • 3
  • 5
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.

JBoulhous
  • 844
  • 7
  • 11
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

Therichpost
  • 1,759
  • 2
  • 14
  • 19