Modify User Role When Easy Digital Downloads (EDD) Subscription is Cancelled

In this tutorial I will show you how to modify the WordPress User Role when an Easy Digital Downloads (EDD) subscription is cancelled. Note that this assumes you are using Easy Digital Downloads with the Recurring Payments extension.

Here is a little information about my specific case use for this:

  • I created a custom WordPress User Role called ‘Field Staff’ (slug -> fieldstaff)
  • I have an application form that accepts the payment, creates the EDD subscription, creates the user account and applies the ‘Field Staff’ user role automatically
  • The ‘Field Staff’ user role gives the subscriber access to specific parts of the site

So, when the user cancels their subscription their WordPress User Role needs to immediately change, which in-turn revokes their access. So, with that said, the function below will automatically change the user’s role to ‘Subscriber’ when they cancel their recurring subscription. This snippet should be added to your functions.php file.

<?php
//Downgrade user role when field staff cancel subscription
function wd_downgrade_fieldstaff( $sub_id, $subscription ) {
	$u = new WP_User( $subscription->customer->user_id );
	$u->remove_role( 'fieldstaff' );
	$u->add_role( 'subscriber' );
}
add_action( 'edd_subscription_cancelled', 'wd_downgrade_fieldstaff', 10, 2 );

What this function does is it finds the User that is linked to the subscription that is being cancelled and removes the ‘Field Staff’ role, then adds the default ‘Subscriber’ role. You will want to make sure you change the “fieldstaff” text to whatever role you want removed. Then the edd_subscription_cancelled action ensures that this function runs when an Easy Digital Downloads subscription is being cancelled.

This simple little function will save the end user a lot of time by removing the tedious task of modifying user role each time someone cancels their subscription.

Comments

  1. David says:

    Awesome.
    Any idea on how to do this in reverse? Specifically, when someone creates an account to assign their user role as “customer”? And also- when someone is officially approved to become a vendor to switch their user role to “Vendor”?

    I’m using a plugin to hide particular menu items based on user role, but EDD doesn’t auto assign the appropriate roles…

    Thanks!! 🙂

    1. Matt Whiteley says:

      Hey David,

      You’d need to look for the actions to hook in to. Depending what you’re doing to create the account and what you’re doing to approve them as a vendor. Find out what actions are fired when those things happen, then hook in to them using the code above (modified with the correct hook).

      Is your account creation and vendor approval functionality all baked-in with Easy Digital Downloads?

      Matt

  2. Matt says:

    Hey Matt,

    Could you elaborate how you created an “application form that accepts the payment, creates the EDD subscription, creates the user account and applies the ‘Field Staff’ user role automatically”.

    Thank you!

    1. Matt Whiteley says:

      Hey Matt, Originally, I had set up a Gravity Form using the EDD add-on. It had some trouble with recurring payments setup so the way it currently works is this: 1. User fills out application (Gravity Form). 2. Admin reviews and if approved triggers an approval email. The approval email includes a link that takes them directly to the EDD cart with the appropriate EDD item already in the card. 3. The cart is set so they must create an account when checking out so they create their user account during purchase of the EDD item. 4. I have a function that modifies user role when a subscription is purchases (see below).

      //Set Field Staff User Role
      function wd_edd_run_when_purchase_complete( $payment_id, $new_status, $old_status ) {
      
          // Make sure that payments are only completed once
          if( $old_status == 'publish' || $old_status == 'complete' ) {
              return;
          }
      
          // Make sure the payment completion is only processed when new status is complete
          if( $new_status != 'publish' && $new_status != 'complete' ){
              return;
          }
      
          $payment_data = edd_get_payment_meta( $payment_id );
          $user_info = maybe_unserialize( $payment_data['user_info'] );
          $currentUser = $user_info['id'];
      
          $user = new WP_User( $user_info['id'] );
          $user->set_role( 'fieldstaff' );
      
      }
      add_action( 'edd_update_payment_status', 'wd_edd_run_when_purchase_complete', 100, 3 );
      
      1. Matt says:

        Thank you for the reply! Quick question, are the approval emails unique to a user/submission? How do you prevent the link with the EDD item already in the cart from others?

        When you say the admin “reviews” and trigger an approval. Through what method are you doing that?

        Something like: https://gravitywiz.com/simple-submission-approval-gravity-forms/
        or: https://wordpress.org/plugins/gravityformsapprovals/

        Your suggestions are very helpful, thank you again!

        1. Matt Whiteley says:

          Technically, nothing would prevent other users from getting the products, but the EDD product has a 301 redirect on it so nobody can access it. So for someone to get it to their cart they would need to know the product ID, variation ID and know how to use add to cart EDD urls to figure it out. Long shot there…so if something figures that out, more power to them!

          The client has a canned email setup in his G-Suite account. So he reads over the application and if approved, used a canned email, swaps out the email and name, and sends it out.

          If all users will be approved regardless you can use something like a delayed Gravity Forms notification to trigger the notification after a set amount of time. That add-on is available by request from Gravity Wiz.

          1. Matt says:

            Thank you so much! Your replies were very helpful. I may see if this add on will accomplish what I need. Your method is very nice too and I am considering it also!

            https://restrictcontentpro.com/downloads/edd-fes-vendor-limits/

  3. Jack says:

    Hi Matt,

    Thanks for sharing this great piece of code. I just started with EDD and i love it this far. I want add data to my database when the payment is successful.

    First of all, i can’t find functions.php. Did EDD changed this?

    Second:
    With your code i wrote a new function. For testing purpose i want to echo some data. I placed this in includes/plugin-compatibility.php.

    //Fire codes when payment received
    function wd_edd_run_when_purchase_complete( $payment_id, $new_status, $old_status ) {

    // Make sure that payments are only completed once
    if( $old_status == ‘publish’ || $old_status == ‘complete’ ) {
    return;
    }

    // Make sure the payment completion is only processed when new status is complete
    if( $new_status != ‘publish’ && $new_status != ‘complete’ ){
    return;
    }

    $payment_data = edd_get_payment_meta( $payment_id );

    $currentUser = $user_info[‘id’];

    if ($download_id == “427”) { // Item ID of file
    echo “Item EDD item ID: 427 added to the database. Purchase by: “.$currentUser;
    }

    }
    add_action( ‘edd_update_payment_status’, ‘wd_edd_run_when_purchase_complete’, 100, 3 );

    Unfortunately the echo isn’t showing up. Can you help me out?

    1. Matt Whiteley says:

      Hey Jack!

      First thing first – never modify core plugin files. Always make changes to your theme (or child theme) or use some sort of functionality plugin to modify behavior. If you edit core files, your changes will be lost when those plugins/themes are updated.

      Second, you need to place this function in the functions.php file in your theme or child theme (it isn’t located in EDD, it would be in a theme or child theme).

      Once you have it in the right place, you can see if it does what you want it to do.

      Without digging in to it further I’m also not sure if your $download_id variable would return anything or not, so if it doesn’t work for you, I would check about pulling in the current download ID differently.

      Cheers,

      Matt

  4. Piyush Arora says:

    Hi,

    Thanks for this informative post.

    Could you kindly guide us in how to deal with expired subscriptions. We need to be able to show expired subscription users some promotional text, prompting them to buy again.

    PS: We’re using EDD content restriction shortcodes with EDD recurring payments to power the platform.

    Thanks,
    Piyush

  5. Nick says:

    Hey Matt,
    Thanks for sharing this very useful snippet of code! I have a concern though: what happens when a subscriber who, let’s say purchases a yearly recurring subscription on 22nd September 2022 (expiration date: 22nd September 2023), cancels it right away because he doesn’t want to have his subscription automatically renewed in a year? Does he lose access to the “premium content” of the site since he canceled his subscription, or does this take into account that although his recurring subscription is canceled, his access to the premium content of the site remained unchanged until 9.22.23?
    Thanks!
    Nick

    1. Matt Whiteley says:

      Hey Nick,

      You would likely need to add some logic to this to check the expiration date of the subscription that was cancelled and set a scheduled task to modify the user role at that time. It is a bit out of the scope of this post, but is certainly doable.

      Matt

Leave a Reply

Your email address will not be published. Required fields are marked *