Changing Roles in WooC based on Order Status

38 views Asked by At

I want to be able to manually confirm offers --> Thereby changing the order status before a user gets assigned a new role on my page. I tried to combine two functions I found here but when I launched them the page crashed. The manual confirmation is important since we have to check each person that wants to purchase a membership. The Roles are important because I use them to limit access to other parts of the website.

A users Journey: A user comes to the website. Then has to log in or create a profile. Then they are forwarded to the site where you can get the membership. (Woocomerce and Stripe) I want to be able to manually confirm if somebody buys a membership or not. If I confirm the user gets a new role. If I don't confirm the user gets no role or is deleted ( the payments should obviously not happen in that case.

What is the problem can somebody please help?

 function uiwc_change_role( $order_id ) {

  // get all the order data

  $order = new WC_Order($order_id);

    $user = $order->get_user();

    $order_status = $order->get_status();  

   if ('complete' == $order_status) {   



    if( false != $user && !user_can($user, 'administrator') ){

// our new role name
$role = 'aktives_mitglied2022';

//set the new role to our customer
$user->set_role($role);     
}    
 //return $order_status;
}  
}
 
//add this newly created function to the thank you page
add_action( 'woocommerce_thankyou', 'uiwc_change_role', 100, 1 );
1

There are 1 answers

0
mujuonly On
 function custom_uiwc_change_role( $order_id ) {

  // get all the order data

  $order = new WC_Order($order_id);

    $user = $order->get_user();

    $order_status = $order->get_status();  

   if ('complete' == $order_status) {   



    if( false != $user && !user_can($user, 'administrator') ){

// our new role name
$role = 'aktives_mitglied2022';

//set the new role to our customer
$user->set_role($role);     
}    
 //return $order_status;
}  
}
 
//add this newly created function to the thank you page
add_action( 'woocommerce_thankyou', 'custom_uiwc_change_role', 100, 1 );