Hello,
I have a forms that is linked to a WC product thanks to a specific custom field integrated inside the form.
The form is published as soon as the product is paid.
Now, I need to change the role of the user that have created the new post and don't know which hook I need to use for that.
Can you helps please?
Regards
Pat
Hi,
It seems that our chat was disconnected so I've moved the conversation to this support ticket so that you can share any update or a follow-up question.
You're welcome to reach us through a new ticket anytime, for a different question or concern.
regards,
Waqar
Hi Waqar,
Thanks for having added this ticket for me.
During our conversation, you proposed to use cred_commerce_after_order_completed
I just want to be sure that this will cover my case. Here some additional explanations :
I have a postype (Adhesion) and have created a Toolset Form to create a post in the frontend. This form is a WC form and I have inside a radio field that is used to define the product that will be associated to the buying process.
As soon as a user validate the form, the post is created and the related product is added to the cart. This part is fully working.
Now, I need to add a hook in order to change the user's role (the user that have created the post) depending of the product he has chosen in the form. I have this hook currently and it is working fine but I have placed it as a cred_save_data which change the role as soon as the form is validated.
What I need is :
1. to make the post published when payment is made (done thanks to the standard WC / Toolset process)
2. to change the user role when the order is completed (either by a payment or by a manual action in the WordPress admin).
Could you confirm how I can manage this?
Regards
Pat
Hi Pat,
Thanks for writing back.
I've performed some tests on my website and can confirm that the "cred_commerce_after_order_completed" hook will work for your second requirement.
( ref: https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_after_order_completed )
An example snippet that I used, for replacing user's "customer" role to "author", once the order gets completed:
add_action( 'cred_commerce_after_order_completed', 'my_hook', 10, 1 );
function my_hook( $data ) {
// check for a specific form
if($data['extra_data'][0]['cred_form_id'] == 123) {
$user_id = $data['user_id'];
$user = new WP_User( $user_id );
// Remove role
$user->remove_role( 'customer' );
// Add role
$user->add_role( 'author' );
}
}
Please replace "123" with the actual post form's ID and "customer" and "author" with the actual current role and the new one, respectively.
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
Hi Waqar
Thanks for helping.
Regards
Pat