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.

About Matt Whiteley

Matt has been building bespoke WordPress websites for over 10 years specializing in the Genesis Framework. He integrates Advanced Custom Fields heavily in all builds, especially with the new Gutenberg Block Editor, allowing clients to easily modify all aspects of the their website with ease.

He works with a wide variety of small businesses and agencies across the country providing development services as well as hosting and maintenance. When he isn't coding he enjoys spending time with his wife and two children, golfing and playing poker.

Reader Interactions

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!! 🙂

    • 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!

    • 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 );
      
      • 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!

        • 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.

  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?

    • 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

Leave a Reply

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