Skip Navigation

[Resolved] Date Field Calculations + Changing User Role Based On User Custom Field Value

This support ticket is created 6 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Author
Posts
#610500

Hi Luo,

I have tested the solution you suggested, however I am still finding an issue.

I have disabled WooCommerce Subscriptions and found the following: the actions I have assigned to cred_commerce_after_order_completed are successful if the product is free, but still do not work if the product has a cost, even if the payment is successful.

The code I am using is below. This code works only if the product is free. Can you advise on what may be causing this issue now that I have disabled WooCommerce subscriptions?

// 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 ( $form_id == 1753 ) {

    	$user_id = get_current_user_id();
    	$user = get_userdata( $user_id );

    	$user->set_role( 'tsn_gold' );
//	}
}
#610579

Please try to modify your PHP codes as below:

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'] == 1711 ) {
        $user_id = $data['user_id'];
        $user = get_userdata( $user_id );
        $user->set_role( 'tsn_gold' );
	}
}

More help:
https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_after_order_completed
$user_id: ID of the user that placed the order.

#611573

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' );
			}
		}
	} 
}
#611615

Thanks for share the codes, that will help other users, I don't think that there is anything could cause major trouble with the standard Toolset features, but you can feedback if there is anything conflict.

#615976

Marked thread as resolved, thanks for your help Luo!