'Remove user when subscription is cancelled by the customer in Woocommerce

When customer click cancel subscription, they still can logged in the my account section, but I want to let user cannot access anything on my account section. From this point, I would like to make the function on "if the user cancels payment, it also delete the account too" to make them cannot access, so I try to investigate and write the code below.

    add_action( 'woocommerce_order_status_cancelled', 
    'custom_woocommerce_auto_delete_user' );

    function custom_woocommerce_auto_delete_user( $order_id ) {
        global $woocommerce;
        $order = new WC_Order( $order_id );
        $order_status = $order->get_status();
       if ( !$order_id )
            return false;
       if ('cancelled' == $order_status) {
         $current_user = wp_get_current_user();
            wp_delete_user( $current_user->ID,true );
       return true;
       }
   return false;
  }

However, I'm just the beginner of woocommerce and I don't know which solution is the best for this issue. I'm so glad for anyone that come to response to me :)



Solution 1:[1]

I want to delete the users who are created when trying to buy a subscription but the payment gets failed although the user anyhow creates. I am just to confirm if this code works for Me or not?

  add_action('woocommerce_subscription_status_failed', 'custom_woocommerce_subscription_status_failed');
function custom_woocommerce_subscription_status_failed($order_id)
{
        global $woocommerce;
        $order = new WC_Order( $order_id );
        $order_status = $order->get_status();
        if ($order_status`enter code here` == 'failed') {
          $current_user = wp_get_current_user();
          wp_delete_user( $current_user->ID,true );
          return true;
        }
        return false;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ali Raza