2

The snippet below links a user's Easy Digital Download (EDD) download payment to allocating a secondary user role to that user.

As per the example below...

  • If a user buys a EDD download with a download ID of 340 then that user will be allocated a secondary user role of "buyer"... or,
  • If a user buys a EDD download with a download ID of 240 then that user will be allocated a secondary user role of "other_role".

I'd like each secondary user role to be unallocated from each user X days after the successful EDD payment.

function pw_edd_run_when_purchase_complete( $payment_id, $new_status, $old_status ) {
        if ( $old_status == 'publish' || $old_status == 'complete' ) {
            return;
        } // Make sure that payments are only completed once

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

        $downloads = edd_get_payment_meta_downloads( $payment_id );
        $user_id   = edd_get_payment_user_id( $payment_id );

        if ( is_array( $downloads ) ) {
            // Increase purchase count and earnings
            foreach ( $downloads as $download ) {
                $user = new WP_User( $user_id );
                if(!$user )
                    continue;

                if( $download['id'] == 340 ) {
                     // Add custom role
                     // Change 340 to the download id of the product that a certain role corresponds
                    $user->add_role( 'buyer' );
                    //also change buyer depending on your role that you created in User Role Editor


                } elseif( $download['id'] == 240 ) {
                     // Add custom role
                      // Change 340 to the download id of the product that a certain role corresponds
                    $user->add_role( 'other_role' );
                    //also change other_role depending on your role that you created in User Role Editor
                }
            }
        }
    }

    add_action( 'edd_update_payment_status', 'pw_edd_run_when_purchase_complete', 100, 3 );
1

As alternative to a cron job, you may use a "just in time" removal of the role.

i.e. when an user logs in, you can check if it has the role(s) you want to expire, check current date with download date and update the user accordingly.

This assumes that you are able to get the date or timestamp of (at least) the last download of an user.

I am not familiar with EDD, but this is a prof of concept code:

add_action( 'wp_login', function($username, $user) {

   $toExpire = [ 'buyer', 'other_role' ];

   $toRemove = array_intersect($user->roles, $toExpire);

   if (count($user->roles) < 2 || empty($toRemove)) {
     // nothing to do if the user has just 1 role or none of the roles you want to expire
     return;
   }

   // the function below does NOT exists
   // I think in EDD you should be able to get the date or timestamp of an user last download
   $downloadTimestamp = get_last_download_timestamp_for_user($user->ID);

   $dayLimit = 30 * DAY_IN_SECONDS; // expire after 30 days

   if ((time() - $dayLimit) < $paymentTimestamp) {
     // do nothing if not enough time passed
     return;
   }

   if (! array_diff($toExpire, $toRemove)) {
     // if an user has ONLY roles that might expire, keep the first role
     array_shift($toRemove);
   }

   // remove expired roles
   foreach($toRemove as $role) {
     $user->remove_role($role);
   }


}, 1, 2);

Doing like so you don't need to set a cron job, and it is easier to implement.

However, using this method, in your system you may see users with "expired" roles just because they have not logged in since the role expired. If you have (or want) statistics in this regard, you need to keep this into account.

|improve this answer|||||
1

You'd need to set up a cron job using wp_cron function.

I would set a custom user field with the date when the secondary role is added. Then when the cron job fires, check that date against todays date. If the expiry is due then remove the secondary user role then.

Is that what you mean?

|improve this answer|||||
  • Yes that is what I want – Pete May 31 '16 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.