Skip Navigation

[Resolved] Member profiles being made active/inactive based on subscription status

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

Problem: I would like to activate or deactivate a custom post based on the membership status of the post owner.

Solution: There's nothing built-in that will do this for you, but there are some APIs you can use to write custom code that will help.

Relevant Documentation:
Subscription status change hook: @https://docs.woocommerce.com/document/subscriptions/develop/action-reference/#section-2

Getting the results of a View in PHP:
@https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results

Update post status:
@https://developer.wordpress.org/reference/functions/wp_update_post/

This support ticket is created 3 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by jeffM-14 3 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1638505

From the client:

"... we still need to find a custom solution to move a member profile from "Published" to "Draft" when the "Author" of the post no longer has an active subscription and then from "Draft" to "Published" when the "Author" is active again..."

Here is the background:

The CAPPA.net website is a global member directory of birthing doulas and trainers. Someone can subscribe to be a member (via WooCommerce Memberships and Subscriptions extensions) here hidden link

Once someone has a paid membership subscription, they are able to create a "member profile" that appears in the directory hidden link. (These profiles are WordPress Custom Post Types controlled via the ToolSet plugins)

Their member profile is created and maintained via their My Account area hidden link.

The issue is that this "CAPPA Member Profile" is a Custom Post Type that is published when the member creates it, but if that member's WooCommerce Subscription expires or they choose to not renew, the profile still stays "published" (since it is not tied directly to WooCommerce).

I need to find a way to automatically make the CAPPA Member Profile entry revert to Draft status should the member's subscription expire or is no longer active.

Would this be possible somehow with ToolSet and the WooCommerce products?

#1639063

Hi, yes I think it's possible but it requires custom PHP. I don't have a cut-and-paste solution available, but I can show you some code examples and point you to the documentation for APIs that will be helpful here. I assume your Member Profile post has some connection to the WordPress User ID of the subscriber whose subscription was changed...either the User is the author of the Member Profile post or there is a custom field in the post or user profile that links these items.

You can use the WooCommerce Subscriptions API to listen for subscription status change events:
https://docs.woocommerce.com/document/subscriptions/develop/action-reference/#section-2
That change event hook gives you access to the new status and the old status, as well as an instance of the corresponding WC_Subscription object:

add_action( 'woocommerce_subscription_status_updated', 'status_update_callback', 1, 3); 

function status_update_callback( $subscription, $old_status, $new_status ) {
  // this function is triggered every time a subscription changes status so you can trigger different code
  // based on whether the subscription is being deactivated, reactivated, or something irrelevant

}

The WC_Subscription Object extends WC_Order, so inside that callback you can get the subscriber's WordPress User ID like so:

$updated_sub_user_id = $subscription->get_user_id();

So far so good. Now you need a way to get the Member Profile post ID from the subscriber ID. If the MP author is the same as the subscriber whose subscription just changed, then you have a few options at this point. You can query posts by post author, arguments something like this:

$args = array(
    'author'        =>  $updated_sub_user_id,
    'post_type'        => 'your-member-profile-post-type-slug',
    'orderby'       =>  'post_date',
    'order'         =>  'ASC',
    'posts_per_page' => 1
    );
$member_profile_post = new WP_Query( $args );

...or you can create a View filtered by post author and use the Views API get_view_query_results: https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results
Change 12345 to match the View ID, and the code looks something like this:

$member_profile_posts = get_view_query_results( 12345 );
$mp_post_id = isset( $member_profile_posts[0]->ID ) ? $member_profile_posts[0]->ID : -1;

I can help you out with either of those options if you need assistance. Now let's assume you have the MP post ID. All you need to do now is change the post status to "draft":

$mp_post_id = 12345; // your member profile post id
$mp_post_args = array( 'ID' => $mp_post_id, 'post_status' => 'draft' );
wp_update_post($mp_post_args);

Let me know if this is making sense or if I have completely confused you.

#1642421

This is great, thank you.

We will try this and let you know. Please keep this ticket open until I get back to you, thanks.

#1646789

Standing by for your update, thanks.

#1649135

Thank You, Christian! Your notes and direction worked. The client confirmed everything.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.