简体   繁体   中英

Change order status to refund when a paid booking is cancelled in Woocommerce booking

I am using Woocommerce with Woocommerce Booking plugin and I would like to update the order status to refunded every time a paid booking is cancelled.

I've found some answers on StackOverFlow, but still can't manage to solve this requirement.

I know I might be completely wrong, but this the last try I made and obviously it didn't work:

add_action('woocommerce_booking_paid_to_cancelled','change_status_to_refund', 10, 2);
function change_status_to_refund($booking_id, $order_id) {
     $booking = new WC_Order($booking_id);
     $order = new WC_Order($order_id);
     $booking_status = $booking->get_status();
     if($booking_status != 'paid'){
        $order->update_status('refund', 'order_note');
     }
 }

Any help is welcome.

You are not using the correct arguments for woocommerce_booking_{ status_from }_to_{ status_to } action hook in your function, which are:

  • The booking ID: $booking_id ,
  • The Booking Object $booking .

So you need to get the Order from the Booking in your code, to be able to update the Order status.

Note: The condition $booking_status != 'paid' is not really needed.

So your code will be much simpler and effective:

add_action('woocommerce_booking_paid_to_cancelled','cancelled_booking_order_status_cahnged_to_refund', 10, 2);
function cancelled_booking_order_status_cahnged_to_refund( $booking_id, $booking ) {$
    // Get the WC_Order object from a booking
    $order = wc_get_order( wp_get_post_parent_id( $booking_id ) );

    // Update order status
    if( is_a($order, 'WC_Order') )
        $order->update_status('refund');
}

Code goes on function.php file of your active child theme (or active theme). It should works.


Documentation: Woocommerce booking developer documentation for filter and action hooks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM