Skip Navigation

[Resolved] Change a user’s role with Cred Commerce on payment and post expiration

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to change a user's role when payment is made through Toolset Forms Commerce and then change it back again when the related post expires.

Solution:
The API hook cred_commerce_after_order_completed can be used to trigger code which changes the user role using the standard set_role method.

Reverting the role when the related post expires is slightly trickier, as there is no Toolset API hook for when a post expires.

In this case, when a post expires it is changed to draft status.

You can use the standard WP hooks that are triggered when changing a custom post status to draft (e.g. the draft_project hook for a project post type) to check when a post of the relevant type is changed to draft status, and then check whether the post expiration time has passed (meaning the change to draft was likely triggered by the expiration) using the value stored in a hidden custom field '_cred_post_expiration_time' that can be retrieved with get_post_meta.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_after_order_completed
https://developer.wordpress.org/reference/classes/wp_user/set_role/
https://codex.wordpress.org/Post_Status_Transitions
https://developer.wordpress.org/reference/functions/get_post_meta/

This support ticket is created 5 years, 3 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by daveG-7 5 years, 3 months ago.

Assisted by: Nigel.

Author
Posts
#1322577

Hi. We have a CRED post form that accepts payment, giving people membership privileges that then expire after a year. Would it be possible for the system to change their user role when payment is accepted, then again when the post expires?

#1322753

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Dave

You should be able to use the hooks provided by the Forms API to change the user role when the order completes.

Specifically, there is the cred_commerce_after_order_completed hook that you could use to trigger code that uses the set_role method of user objects: https://developer.wordpress.org/reference/classes/wp_user/set_role/

https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_after_order_completed

It's less obvious what to do about changing the role back, because there is no hook available when the post expires. (It seems to me there should be, so I just created a request for one.)

You'll need to use built-in WordPress hooks that would be triggered by the action that occurs when the post is expired. So I would need to know what your form settings are for what happens when a post is expired, e.g. status is changed to...?

#1323251

Thanks, that's a great start. As for expiration, the post status is changed to draft. However, there are other instances when a post is changed to draft where we probably wouldn't want the user role changed, so if it were possible to make it only happen to posts that were changed to draft by expiration, that would be ideal. However, as you say, there's no hooks for that, so if it's only possible when posts are changed to draft, that still might be better than it not changing it at all.

#1324175

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

There are several WP hooks that could be used here (see https://codex.wordpress.org/Post_Status_Transitions), and I would be inclined to go with the {status}_{post_type} hook, which is triggered whenever the status of a post of the specified type changes to the specified status.

Not sure what your post type is, but say it were 'project', then the draft_project hook would be fired whenever a project post were changed to draft.

Now, this would catch all such post transitions, not just those where the post was expired.

But it should be simple enough to test the post expiration date against the current date-time. If the post expiration date has passed already, then we can assume the post was expired.

The post expiration date is available as a timestamp from the postmeta field with a key of '_cred_post_expiration_time'.

#1326589

I think that points me in the right direction. If I encounter other issues further down the road, I'll start a new thread. Thanks.