Skip Navigation

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

This support ticket is created 6 years, 11 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
#603538

I'm in the process of creating a site with 3 different membership levels / User Roles (Bronze, Silver and Gold) using the instructions at https://toolset.com/documentation-category/cred-commerce/.

Bronze will be free to sign up to, Silver will be paid annually and Gold will be paid monthly.

For Gold Subscription management I'm using WooCommerce Subscriptions and have managed to set this up successfully using the documentation at https://toolset.com/learn/create-membership-site-wordpress-using-toolset-plugins/how-to-use-the-woocommerce-subscriptions-plugin-with-toolset/

For Silver, I have begun with the documentation at https://toolset.com/documentation/user-guides/charging-payments-with-cred-to-register-users/ in order to process the purchase and auto create the user with "Silver" role, though I have just one issue, as below…

I need the user role to be applied for one year from the date of purchase, then downgraded if not manually renewed after that year. I understand this is not possible with the standard setup as per your documentation though I have a potential workaround in mind that you may be able to help with.

Currently, in order to sign up to Silver membership, the User is created through a CRED form which then leads to a product purchase.

In that user form there could be two custom user fields: "Silver Member Since" which will be automatically populated with the date the form is filled in (i.e. the date of purchase) and "Silver Membership Valid Until" which is populated with the date the form is filled in + 1 year.

I then envisage a separate CRED Form ("Silver Renewal Form") which edits the current user and a separate product ("Silver Renewal Product"). The Silver Renewal Form would be used to update the value of the field "Silver Membership Valid Until" (for the current user) with its existing value +1 year (upon successful purchase of the Renewal product).

Aside from calculating the date field values, the part of the above I would need most help with is using custom php to check the value of the field "Silver Membership Valid Until" and downgrade the User Role from "Silver" to "Bronze" if that date is in the past.

Is the above possible?

I'm aware this doesn't consider is any kind of upgrade process from Bronze to Silver or a downgrade from Gold to Silver, but it would be useful to know if the first step detailed above is possible before I go too far.

Thanks in Advance!

#603654

Dear Jon,

Yes, you are right, there isn't such kinds of built-in feature within CRED commerce yet, and it has already been put in our to-do list, our developers are working on it.

Currently, it is possible with custom codes, for example,
1) you can create a custom user date field to store the expiration time value:
https://toolset.com/documentation/user-guides/user-fields/
2) When user submit the CRED form for creating the Silver Member, and after they complete the woocommerce order, use CRED action hook "cred_commerce_after_order_completed" to update above date field value as current time:
https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_after_order_completed
3) Create a wordpress CRON job to query all "Silver" users, whose date field value is outdated, then update his user role to "Bronze":
https://developer.wordpress.org/reference/functions/wp_schedule_event/
https://developer.wordpress.org/reference/classes/wp_user/set_role/

For your reference

#604712

Thankyou for this, I am currently awaiting help on another support ticket and when that ticket is resolved I will be able to begin testing the process you describe above.

I will update you on progress as soon as I have been able to move forward.

#604925

OK, please update this thread when you still need assistance. thanks

#606703

Okay, I should be able to get working on this tomorrow now as the other ticket is likely resolved. I'll update you when I have tested your instructions.

#606962

Please update this thread when you still need assistance. thanks

#608730

Hi Luo, sorry for the delay on this.

The first question I have is how do I get the ID of the User Account that the CRED Commerce form is used to create?

The problem is the new user does not log in at any point during the CRED process so I don't think I can use the 'current' user ID. I don't think I can use the ID of the user who placed the order either because they were a 'Guest User' when they placed it.

So far I have the code below but it doesn't work - I imagine because $user_id is not defined.

Can you advise on how to get the ID of the new user account?

// Update Silver Member Renew Date
add_action( 'cred_commerce_after_order_completed', 'update_silver_member_renewal_date', 10, 1 );
function update_silver_member_renewal_date( $data ) {    
	$time = current_time( $timestamp, $gmt = 0 );
	//Update User Custom Field
	update_user_meta( $user_id, $wpcf-silver-account-last-renewed, $time );
}

Thanks in Advance!

#608774

Yes, you are right, since it is not a built-in feature of CRED plugin, and it needs custom codes. here is my suggestion, you can let the new user create a lower user role, for example "subscriber", login and use CRED form for editing their user role as Silver Member, then use CRED action hook "cred_commerce_after_order_completed" to update the date field value as current time, you will be able to get the user's ID with wordpress function get_current_user_id():
https://developer.wordpress.org/reference/functions/get_current_user_id/

#608950

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?

#609169

Since it is a custom PHP codes problem, please provide a test site with same problem, fill below and fill below private detail box with login details and FTP access, also point out the problem page URL and CRED form URL, and where I can edit you PHP codes, I need a live website to test and debug, thanks

#609347

As your request, I am downloading the duplicator package file, it take more time to debug this problem, and I will update this thread if there is anything found

#609605

You are using plugin "WooCommerce Subscriptions", it prevent CRED commerce action hook "cred_commerce_after_order_completed", you can disable the plugin "WooCommerce Subscriptions", and test "Upgrade to Silver" again, I just tried these in your website:
1) disable the plugin "WooCommerce Subscriptions"
2) Edit the product "Silver Membership", setup the price as 0,
then it works fine, both the custom date field value and user's role

and there are some problem in your PHP codes:

// 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) { 

According to our document:
https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_after_order_completed

There is only one Argument "$data", see the example codes here:
https://toolset.com/forums/topic/cred-commerce-api-hooks-not-firing/

#609629

Hi Luo,

Thanks for the update, I will test this, though it does unfortunately cause a bigger problem.

I had been following your documentation at https://toolset.com/learn/create-membership-site-wordpress-using-toolset-plugins/ in order to build the site and for our higher tier (Gold User) WooCommerce Subscriptions is absolutely necessary unless another subscription solution is Available.

The only workaround I can think of is running a basic CRED Commerce payment without any account changes, then adding a CRED form to the Custom Thankyou page message that uses CRED_save_data to make the account changes. That way only those who've payed successfully will see the form that actually upgrades their account. Do you think that would work?

Can you think of any other possible solutions?

Thanks in advance

#609681

Thanks for the details, I am downloading the files, will feedback if there is anything found

#610041

For the question:
That way only those who've payed successfully will see the form that actually upgrades their account. Do you think that would work?
It depends on yourself, and you are right, it is possible to work, but in my opinion, the best option is without plugin "WooCommerce Subscriptions", just use Access plugin to create the custom user role, and use CRED commerce plugin to update the user's role, and extend the expiration date.
But this feature is still under development, I am not sure when will it be released, I suggest you subscribe to our blog to get the updated news:
https://toolset.com/blog/