Ask your WordPress questions! Pay money and get answers fast! (more info)

EDD: Auto Add to/Edit a logged-in user's role capabilities WordPress

  • SOLVED

I'm using the [[LINK href="https://wordpress.org/plugins/easy-digital-downloads/"]]Easy Digital Downloads[[/LINK]] plugin

I'm looking for a function whereby..

After successful payment the logged in user gets a secondary role added/assigned to them (I'm using the [[LINK href="https://wordpress.org/plugins/user-role-editor/"]]User Role Editor[[/LINK]] plugin for the secondary roles) <strong>OR</strong> an extra capability added/assigned to them... pros/cons?

This way I can apply normal WP conditionals into my theme templates and page content to restrict non paid logged in users.

Thanks, Pete

Answers (3)

2015-10-11

Romel Apuya answers:

Hi,

Is the payment one time? or per products??
a hook to edd_update_payment_status this might work.

function ao_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 );
$email = maybe_unserialize( $payment_data['user_email'] );
$downloads = maybe_unserialize( $payment_data['downloads'] );
$user_info = maybe_unserialize( $payment_data['user_info'] );
$cart_details = maybe_unserialize( $payment_data[‘cart_details’] );
if( is_array( $downloads ) ) {
// Increase purchase count and earnings
foreach( $downloads as $download ) {
// Get product ID
$download_id = $download['340'];
//
// Do stuff here for each product purchased
global $current_user;
get_currentuserinfo($user_id);
$user = new WP_User($user_info['id']);
$user->add_role( 'buyer' );
}
}
}

add_action( 'edd_update_payment_status', 'ao_edd_run_when_purchase_complete', 100, 3 );



[[LINK href="https://easydigitaldownloads.com/forums/topic/update-or-set-user-role-on-complete-purchase-2/"]]here [[/LINK]]is the link to the situation..


or if you want me to do it.. please send me ftp and wp-admin details.


pjeaje comments:

@Romel Apuya I prefer this compatible with all payment types. I'd rather you test it on your own server first please. I don't like to test it on my site sorry.


Romel Apuya comments:

hi,

I think really the hook to explore is edd_update_payment_status,

so here's a code sample below... with explanation..

I know its Hard to give out FTP or other details...

but if you want it solve you can cooperate, if not.. well its up to you anyways.


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


pjeaje comments:

@ Romel Apuya I like your solution as it gives me an option to allocate 2 different roles according to the download product.


pjeaje comments:

Is there a way you could remove the secondary user role from all user who paid for a specific download ID after X days?

2015-10-11

Rempty answers:

I modified the code from https://easydigitaldownloads.com/forums/topic/update-or-set-user-role-on-complete-purchase-2/ ,
And cleaned it, you need to add this to your functions.php.
User Role Editor + Easy Digital Downloads

function ao_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'] );
$user = new WP_User($user_info['id']);
$user->add_role( 'contributor' ); //Here add the role that you want as secondary
}
add_action( 'edd_update_payment_status', 'ao_edd_run_when_purchase_complete', 100, 3 );

I test this on localhost using the test payment gateway. And the current user get a secondary role.


pjeaje comments:

Thanks, I'm going to bed now. I will test it tomorrow.


pjeaje comments:

Bingo Rempty!


pjeaje comments:

Do you know if this works with reoccurring payments too? Thatbis, i re occurrance of payment doesn't occur then the secondary user role is removed.


pjeaje comments:

Could you write a simple independent function that just removes user role "XYZ" from all users after Z days?

2015-10-11

Tache Madalin answers:

If you're using some sort of confirmation page that's where that could happen as long as the page gets the user id or email.