Thank you for this code, it seems to be able to do just what I need.
But it does not work for me now, copy-pasting it leads to a whitescreen error. There must be a small issue, but I can't find this it in the code you suggest.
On the side - I was thinking: maybe it is better to use the (working) code to cred_commerce_after_order_completed ? That way the role is changed only after completing the whole process.
Try this new code and yes the woocommerce hook will work better .
add_action('cred_commerce_after_order_completed','my_hook',10,1);
function my_hook($data)
{
// if a specific form
if ($form_data['id']==12)
{
$current_user = wp_get_current_user();
$user_arr = array( 'ID' => $current_user->ID, 'role' => 'my new role') ;
wp_update_user( $user_arr);
}
}
I did not get it to work yet, tried different things. I dived into the CRED commerce documentation at https://toolset.com/documentation/user-guides/cred-commerce-api/ and I think the cred_commerce_after_order_completed hook does not use the $current_user->ID variable, so that could be the reason?
I tried with user_id, but it doesn't work either. I don't see where this goes wrong, could you help?
I tried it completely logged out as a guest. It is not working.
The CRED form creates the CPT with the correct user, leads through Woocommerce for account creation and sends all the needed mails. So far so good... only the user role is not changing.
If you wish, you could use the login details I provided earlier to access the admin and test for yourself. If you need more details, or FTP Access, please open a private reply, so I can provide it.
The CRED form ID is #53
The role it is supposed to change to is 'zw*******er'
I have added it to the functions.php of the child theme. For some reason, this child theme was de-activated and the parent theme was activated (I think it happend after reinstalling the theme in an attempt to solve another issue I am having in another thread).
I activated the child theme again and tested the form - the code to update the user role it still is not working.