Connect with us

Change WooCommerce user role on purchase

If you have subscription package in woocomerce and want to set user role according than use below code:

function supplier_change_role( $order_id ) {
  // get all the order data
  $order = new WC_Order($order_id);
  
  //get the user email from the order
  $user = $order->get_user();
    
  // if the this is a registered user and this user is not an admin
  if( false != $user && !user_can($user, 'administrator') ){
  
    // our new role name
    $role = 'woocomerce-supplier';
  
    //set the new role to our supplier
    $user->set_role($role);  
   
  }  
}
 
//add this newly created function to the thank you page
add_action( 'woocommerce_thankyou', 'supplier_change_role', 100, 1 );

Your Answer

Please login or Register to submit your answer
Not the answer you are looking for? Browse other questions tagged or ask your own question.

Log In

Subscribe to our newsletter

Enter your email address to subscribe to blog and receive notifications of new articles by email.
100% Privacy. No spam guaranteed