Hi Luo,
I've made progress but am struggling to make the change from cred_save_data to cred_commerce_after_order_completed
More detail on the two areas I'm struggling with are as follows...
Updating the User date field
When testing without CRED Commerce (a normal Edit User CRED Form) I was able to update the user date field successfully with the following code...
// Update Silver Member Renew Date (Test form - standard CRED Edit User)
add_action( 'cred_save_data', 'update_silver_member_renewal_date', 10, 1 );
function update_silver_member_renewal_date( $data, $form_data ) {
$user_id = get_current_user_id();
$time = date("U");
//Update User Custom Date Field
update_user_meta( $user_id, 'wpcf-silver-account-last-renewed', $time );
}
However, when I then try to implement that code with the cred_commerce_after_order_completed (code below) instead of cred_save_data I am unable to get it working.
// Update Silver Member Renew Date (Live form with CRED Commerce)
add_action( 'cred_commerce_after_order_completed', 'update_silver_member_renewal_date', 10, 1 );
function update_silver_member_renewal_date( $data ) {
$user_id = get_current_user_id();
$time = date("U");
//Update User Custom Date Field
update_user_meta( $user_id, 'wpcf-silver-account-last-renewed', $time );
}
Updating the User Role
When testing without CRED Commerce (a normal Edit User CRED Form) I was able to update the user role successfully with the following code...
// Set User Roles as TSN_Silver (Test form - standard CRED Edit User)
add_action('cred_save_data', 'update_user_role_to_silver',10,2);
function update_user_role_to_silver($post_id, $form_data) {
if ($form_data['id']=='1715') {
$user_id = get_current_user_id();
$user = get_userdata( $user_id );
// set user role to tsn_silver
$user->set_role( 'tsn_silver' );
}
}
However, when I then try to implement that code with the cred_commerce_after_order_completed (code below) instead of cred_save_data I am unable to get it working. (Note the Live form ID is different to the test one so this isn't the issue)
// Set User Roles as TSN_Silver (Live form with CRED Commerce)
add_action('cred_commerce_after_order_completed', 'update_user_role_to_silver',10,1);
function update_user_role_to_silver($data, $form_data) {
if ($form_data['id']=='1711') {
$user_id = get_current_user_id();
$user = get_userdata( $user_id );
// set user role to tsn_silver
$user->set_role( 'tsn_silver' );
}
}
Can you advise on what may be the issue here?