WooCommerce Hook woocommerce_cancelled_order

Good Day,

This is my first time using stackoverflow, nice to meet you all.

Anywho, I'm writing a plugin for WooCommerce and I am automatically refunding an order when it is cancelled. My code works fine when I manually execute it in a separate file without the hook, however, with my hook it seems to not execute. I'm doing the following:

add_action('woocommerce_cancelled_order','change_status_to_refund', 10, 1);
function change_status_to_refund( $order_id ) {
wp_redirect( home_url() );
   $order = new WC_Order( $order_id );

if( 'refunded' == $order->get_status() ) {
     return false;
 }


if(!($order->is_paid())) {
    return false;
}


$noRefundLimit = 24 * 60; //in minutes until booking
$customer_orders = get_posts( array(
 'numberposts' => 1,
 'post_parent' => $order_id,
 'post_type'   => 'wc_booking', // WC orders post type
 'post_status' => 'paid, complete' // Only paid, completed bookings
 ) );
 $bookingId = current($customer_orders)->ID;

 $bookingStart = current(get_post_meta($bookingId, "_booking_start"));
 $time = (new DateTime($bookingStart, new 
 DateTimeZone("America/Los_Angeles")))->getTimestamp();
 $nowTime = (new DateTime())->getTimestamp();
 $difference = round(($time - $nowTime)/60);//in minutes

 if($difference >= $noRefundLimit) {
     $refundPercentage = 1; //how much will we give back? fraction of 1.
     // Get Items
     $order_items   = $order->get_items();

     // Refund Amount
       $refund_amount = 0;
     // Prepare line items which we are refunding
     $line_items = array();

     if ( $order_items ) {
       foreach( $order_items as $item_id => $item ) {
        $refund_amount += $item->get_total();
       }
     }
     $refund_amount = ($refund_amount * $refundPercentage);
     $refund_reason = "Order Cancelled";
     $refund = wc_create_refund( array(
       'amount'         => $refund_amount,
       'reason'         => $refund_reason,
       'order_id'       => $order_id,
       'line_items'     => $line_items,
       'refund_payment' => true
       ));

       var_dump($refund);

       $order->update_status('wc-refunded', 'Order Cancelled And Completely 
Refunded');
       $order->save();
}

I have the first redirect line added for the sole purpose of testing if it redirects, which it doesn't! Any idea why this hook isn't firing?

Thank you for your comment. It seems that the woocommerce_order_status_cancelled hook is good but the parameter is $order_id. After a tremendous amount of debugging I have the following:

add_action( 'woocommerce_order_status_cancelled', 'change_status_to_refund', 
21, 1 );
function change_status_to_refund( $order_id ) {

$order = new WC_Order( $order_id );      

$noRefundLimit = 24 * 60; //in minutes until booking

global $wpdb;
$querystr = "
SELECT $wpdb->posts.* 
FROM $wpdb->posts 
WHERE $wpdb->posts.post_parent = $order_id
";

$pageposts = $wpdb->get_results($querystr, OBJECT);

 $bookingId = current($pageposts)->ID;

 $bookingStart = current(get_post_meta($bookingId, "_booking_start"));
 $time = (new DateTime($bookingStart, new 
 DateTimeZone("America/Los_Angeles")))->getTimestamp();
 $nowTime = (new DateTime())->getTimestamp();
 $difference = round(($time - $nowTime)/60);//in minutes

 if($difference >= $noRefundLimit) {
     $refundPercentage = 1; //how much will we give back? fraction of 1.
     // Get Items
     $order_items   = $order->get_items();

     // Refund Amount
       $refund_amount = 0;
     // Prepare line items which we are refunding
     $line_items = array();

     if ( $order_items ) {
       foreach( $order_items as $item_id => $item ) {
        $refund_amount += $item->get_total();
       }
     }
     $refund_amount = ($refund_amount * $refundPercentage);
     $refund_reason = "Order Cancelled";
     $refund = wc_create_refund( array(
       'amount'         => $refund_amount,
       'reason'         => $refund_reason,
       'order_id'       => $order_id,
       'line_items'     => $line_items,
       'refund_payment' => true
       ));

       $order->update_status('wc-refunded', 'Order Cancelled And Completely 
        Refunded');
     }
}

I had a few problems. $order->is_paid() returns false even though the order is paid (I think this is because the status changed from paid to cancelled/refunded) so my code wasn't even getting that far. Then get_posts wasn't working as expected. I was forced to use $wpdb. After that my code worked.

Use proper action woocommerce_order_status_cancelled.

Here is the example

add_action( 'woocommerce_order_status_cancelled', 'change_status_to_refund', 10, 1 );

public function change_status_to_refund( $order_id ){
    //Do Something here
}