digisavvy
3/16/2016 - 10:22 PM

When registering using from EDD checkout, we the user to a specific role.

When registering using from EDD checkout, we the user to a specific role.

// When registering using from EDD checkout, we the user to a specific role.
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'] == 5004 ) {
                 // Add custom role
                $user->add_role( 'novel_purchase' );
            } elseif( $download['id'] == 240 ) {
                 // Add custom role
                $user->add_role( 'subscriber' );
            }

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