Suspend/unsuspend user based on WP membership status

I’m looking for guidance on how to:

  • suspend the discourse user when the WP subscription expires or is cancelled, and
  • unsuspend the discourse user when the previously expired subscription is renewed.

WP is the SSO provider. My site has been using @simon’s dcpmp code, but I’m going to switch from PMPro to WooCommerce + WC Subscriptions + WC Memberships.

The parts of dcpmp that add/remove the user from a group are not needed in this case.

WC Subscriptions reference:
Subscriptions Action Reference - WooCommerce Docs
Introduction to Subscriptions Developer Documentation - WooCommerce Docs
https://prospress.github.io/subscriptions-rest-api-docs/

1 Like

Have you had a look at WordPress Membership Integration (Private Forum) with Discourse? I’m pretty sure that it walks you though it.

1 Like

I have, but that method doesn’t suspend the user in discourse which means they will still get posts by email and can interact with the forum that way.

Oh. I see. I don’t see that the plugin has a function to suspend the user (but may not know where to look). Worst can you can add an API call to the hook that gets called when their subscription fails and have it suspend the user. You can find it using How to reverse engineer the Discourse API

2 Likes

Suspending and unsuspending users will be the easy part. The trick is to figure out which Woocommerce actions to hook into.

It looks like the woocommerce_subscription_status_updated action should work. That hook passes three parameter: $subscription, $new_status, $old_status. The status parameters are strings, so their value should be fairly self-explanatory. The $subscription parameter is a WC_Subscription object. I’m assuming that it contains details that let you find the user ID that the subscription is associated with. I’d have to take a look at it to confirm though.

3 Likes

Perhaps these:
Subscriptions Action Reference - WooCommerce Docs


Action: 'woocommerce_subscription_status_active'

Parameters: $subscription A WC_Subscription object representing the subscription that just had its status changed.

Description: This action is triggered after the subscription specified with $subscription has had its status changed to activated. The subscription’s status may have transitioned from pending to active, or on-hold to active or some other custom status to active.


Action: 'woocommerce_subscription_status_cancelled'

Parameters: $subscription A WC_Subscription object representing the subscription that just had its status changed.

Description: This action is triggered after the subscription specified with $subscription has had its status changed.

The subscription’s status may have transitioned from pending to cancelled, or on-hold to cancelled, pending cancellation to cancelled or some other custom status to cancelled.


Action: 'woocommerce_subscription_status_expired'

Parameters: $subscription A WC_Subscription object representing the subscription that just had its status changed.

Description: Triggered when a subscription reaches the end of its term, if a length was set on the subscription product when it was purchased or an end date was otherwise set on a subscription.

I’ll try to run some tests to see what they look like.

1 Like