0

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

NYBL
  • 1
  • 2
  • What exactly do you mean by "it doesn't work"? – Nico Haase Jul 05 '18 at 10:14
  • @NicoHaase I mean that I try to click cancel on order of my account page, and the user still can logged in. – NYBL Jul 05 '18 at 10:29
  • Can you explain further and add context to the question? Is the given code even called? In which cases does the execution not match your expectation? – Nico Haase Jul 05 '18 at 10:33
  • @NicoHaase Can we make custom function on delete user when they cancel payment? – NYBL Jul 06 '18 at 03:34
  • Probably you can - just write some code for it ;) And as you already did so, what is the problem with the given code? – Nico Haase Jul 06 '18 at 07:10

1 Answers1

0

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;
}