Reputation: 51
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');
}
Upvotes: 5
Views: 2589
Reputation: 100
//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' );
}
}
Upvotes: 0
Reputation: 31
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 );
Upvotes: 2
Reputation: 1815
Hey you can try this hook!!
https://therichpost.com/change-product-order-status-woocommerce-hook
Hope this will help you
Upvotes: -1
Reputation: 952
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.
Upvotes: 0
Reputation: 473
You need to fetch the status of your order and then check your required condition and update it accordingly.
$order_status = $order->get_status();
Upvotes: 0
Reputation: 1502
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 :)
Upvotes: 6