Some months ago I talked about how to add a checkout field only for a specific user role with WooCommerce.
Today I’ll show you how to change the user role after purchasing a specific product.
Open your functions.php file in wp-content/themes/your-child-theme-name/ and add this code at the end of the file:
Note the code on line 6. It is a list of products to check, which means that the custom script will check if one of the products purchased by the customer is in that list. If yes, the user role will change to what you define on line 14.
Line 13 removes the customer’s old role, by default it’s Customer when they buy from you, but it could be different. If you want to remove more than one role, just duplicate the line and change the role slug.
Note: If you defined multiple role switch and the customer purchased more products that will change their role, only the first one found will switch their role.
The original code is by troydean.