Hi Luo,
Thanks for the correction - I've managed to build the code below to manage the User Roles.
I constructed it as carefully as possible and most eventualities have been tested as working. Despite that, I wonder if you'd mind taking a quick look at what's below to see if you think I have written anything that could cause major trouble with the standard Toolset features? Also if you have any recommendations they would be most welcome!
Update Renewal Date Fields After CRED Purchase
// Update membership renewal dates after new order is completed - GOLD - WORKING
add_action( 'cred_commerce_after_order_completed', 'update_member_renewal_date_gold', 10, 1 );
function update_member_renewal_date_gold( $data ) {
if ( isset($data['extra_data'][0]['cred_form_id']) && ( $data['extra_data'][0]['cred_form_id'] == 1753 ) || ( $data['extra_data'][0]['cred_form_id'] == 1711 ) ) {
$user_id = $data['user_id'];
$product_id = $data['extra_data'][0]['cred_product_id'];
$form_id = $data['extra_data'][0]['cred_form_id'];
// Get existing Account Valid Until date field timestamp
$vu = types_render_usermeta( "account-valid-until", array( "user_id" => "$user_id", "output" => "raw" ) );
$alr = types_render_usermeta( "account-last-renewed", array( "user_id" => "$user_id", "output" => "raw" ) );
// If customer has not purchased a membership before, populate empty fields
// If user fields are already populated, update the field values by extending the existing values
if (empty($vu)) {
$time = date("U");
// Populate Account Last Renewed with Current Date
update_user_meta( $user_id, 'wpcf-account-last-renewed', $time );
$plussixmonths = mktime(0, 0, 0, date("m")+6, date("d"), date("Y"));
$plusoneyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
if ( (( $form_id == 1753) && ( $product_id == 1754 )) || (( $form_id == 1711 ) && ( $product_id == 1756 )) ) {
update_user_meta( $user_id, 'wpcf-account-valid-until', $plussixmonths );
} elseif ( (( $form_id == 1753) && ( $product_id == 1755 )) || (( $form_id == 1711 ) && ( $product_id == 1757 )) ) {
update_user_meta( $user_id, 'wpcf-account-valid-until', $plusoneyear );
}
} else
if (!empty($vu)) {
// Populate Latest Account Renewal date field with previous value of the field Account Valid Until
update_user_meta( $user_id, 'wpcf-account-last-renewed', $vu, $alr );
// Extract timestamp elements
$year = types_render_usermeta( "account-valid-until", array( "user_id" => "$user_id", "format" => "Y" ) );
$month = types_render_usermeta( "account-valid-until", array( "user_id" => "$user_id", "format" => "n" ) );
$day = types_render_usermeta( "account-valid-until", array( "user_id" => "$user_id", "format" => "j" ) );
$hour = types_render_usermeta( "account-valid-until", array( "user_id" => "$user_id", "format" => "H" ) );
$minute = types_render_usermeta( "account-valid-until", array( "user_id" => "$user_id", "format" => "i" ) );
$second = types_render_usermeta( "account-valid-until", array( "user_id" => "$user_id", "format" => "s" ) );
// If 6 month membership is purchased elseif 12 month membership is purchased
if ( (( $form_id == 1753) && ( $product_id == 1754 )) || (( $form_id == 1711 ) && ( $product_id == 1756 )) ) {
$a = array($month,6);
$month_plus_six = array_sum( $a );
$vu_plussixm = mktime($hour, $minute, $second, $month_plus_six, $day, $year);
update_user_meta( $user_id, 'wpcf-account-valid-until', $vu_plussixm, $vu );
} elseif ( (( $form_id == 1753) && ( $product_id == 1755 )) || (( $form_id == 1711 ) && ( $product_id == 1757 )) ) {
$b = array($year,1);
$year_plus_one = array_sum( $b );
$vu_plusoney = mktime($hour, $minute, $second, $month, $day, $year_plus_one);
update_user_meta( $user_id, 'wpcf-account-valid-until', $vu_plusoney, $vu );
}
}
}
}
Update User Role After CRED Purchase
// Set User Role as TSN_Silver (if downgrading from Gold, keep Gold status until renewal date)
add_action('cred_commerce_after_order_completed', 'update_user_role_to_silver',10,1);
function update_user_role_to_silver( $data ) {
if ( isset($data['extra_data'][0]['cred_form_id']) && $data['extra_data'][0]['cred_form_id'] == 1711 ) {
$user_id = $data['user_id'];
$time = date("U");
$alr = types_render_usermeta( "account-last-renewed", array( "user_id" => "$user_id", "output" => "raw" ) );
if ( $alr <= $time ) {
$user = get_userdata( $user_id );
$user->set_role( 'tsn_silver' );
}
}
}
// Set User Role as TSN_Gold
add_action('cred_commerce_after_order_completed', 'update_user_role_to_gold',10,1);
function update_user_role_to_gold( $data ) {
if ( isset($data['extra_data'][0]['cred_form_id']) && $data['extra_data'][0]['cred_form_id'] == 1753 ) {
$user_id = $data['user_id'];
$user = get_userdata( $user_id );
$user->set_role( 'tsn_gold' );
}
}
The CRON Job to verify User Role validity and downgrade if expired
// Check Silver User Account Renewal Status and Update User Role Accordingly
add_action( 'check_silver_user_role_valid_cron_hook', 'check_silver_user_role_valid' );
if ( ! wp_next_scheduled( 'check_silver_user_role_valid_cron_hook' ) ) {
wp_schedule_event( time(), 'hourly', 'check_silver_user_role_valid_cron_hook' );
}
function check_silver_user_role_valid() {
// The Query
$user_query = new WP_User_Query( array( 'role' => 'tsn_silver', 'fields' => 'all' ) );
$today = date("U");
// User Loop
if ( ! empty( $user_query->get_results() ) ) {
foreach ( $user_query->get_results() as $user ) {
$validuntil = types_render_usermeta( "account-valid-until", array( "user_id" => "$user->ID", "output" => "raw" ) );
$user_id = $user->ID;
if($validuntil > $today) {
update_user_meta( $user_id, 'wpcf-test-user-valid', 'User Account Remains Valid as Silver' );
}
if($validuntil < $today) {
update_user_meta( $user_id, 'wpcf-test-user-valid', 'User Account Was Downgraded from Silver to Bronze' );
$user = get_userdata( $user_id );
$user->set_role( 'tsn_bronze' );
}
}
}
}
// Check Gold User Account Renewal Status and Update User Role Accordingly
add_action( 'check_gold_user_role_valid_cron_hook', 'check_gold_user_role_valid' );
if ( ! wp_next_scheduled( 'check_gold_user_role_valid_cron_hook' ) ) {
wp_schedule_event( time(), 'hourly', 'check_gold_user_role_valid_cron_hook' );
}
function check_gold_user_role_valid() {
// The Query
$user_query = new WP_User_Query( array( 'role' => 'tsn_gold', 'fields' => 'all' ) );
$today = date("U");
// User Loop
if ( ! empty( $user_query->get_results() ) ) {
foreach ( $user_query->get_results() as $user ) {
$alr = types_render_usermeta( "account-last-renewed", array( "user_id" => "$user->ID", "output" => "raw" ) );
$validuntil = types_render_usermeta( "account-valid-until", array( "user_id" => "$user->ID", "output" => "raw" ) );
$user_id = $user->ID;
if ( ( $validuntil > $today ) && ( $alr > $today ) ) {
update_user_meta( $user_id, 'wpcf-test-user-valid', 'User Account Remains Valid as Gold' );
}
if ( ( $validuntil < $today ) && ( $alr < $today ) ) {
update_user_meta( $user_id, 'wpcf-test-user-valid', 'User Account Was Downgraded Gold to Bronze' );
$user = get_userdata( $user_id );
$user->set_role( 'tsn_bronze' );
}
}
}
}