Skip to content

Instantly share code, notes, and snippets.

@amdrew
Created May 5, 2014 01:10
Show Gist options
  • Save amdrew/4921c54cbcb94d2ed44e to your computer and use it in GitHub Desktop.
Save amdrew/4921c54cbcb94d2ed44e to your computer and use it in GitHub Desktop.
Easy Digital Downloads - Manual Purchases. Create a new user account when making a manual payment
<?php
function sumobi_eddmp_create_payment( $data ) {
if ( wp_verify_nonce( $data['edd_create_payment_nonce'], 'edd_create_payment_nonce' ) ) {
// email address of user
$email_address = $data['user'];
// check to see if a user account doesn't already exist with email address
$user_id = username_exists( $email_address );
// user doesn't exist, let's create it
if ( ! $user_id && email_exists( $user_id ) == false ) {
// Generate the password and create the user
$password = wp_generate_password( 12, false );
$user_id = wp_create_user( $email_address, $password, $email_address );
// return if user_id already exists
if ( is_wp_error( $user_id ) ) {
return;
}
$first_name = isset( $data['first'] ) ? $data['first'] : '';
$last_name = isset( $data['last'] ) ? $data['last'] : '';
// create user with information entered at checkout
wp_update_user(
array(
'ID' => $user_id,
'first_name' => $first_name,
'last_name' => $last_name,
'nickname' => $first_name, // set nick name to be the same as first name
'display_name' => $first_name, // set display name to be the same as first name
)
);
// Set role
$user = new WP_User( $user_id );
$user->set_role( 'subscriber' );
// User details
$user_details = get_user_by( 'email', $email_address );
$username = $user_details->user_login;
// Subject line
$subject = 'Your login details';
// Build message
$message = edd_get_email_body_header();
$message .= sumobi_eddmp_build_email( $first_name, $username, $password );
$message .= edd_get_email_body_footer();
// get from name and email from EDD options
$from_name = isset( $edd_options['from_name'] ) ? $edd_options['from_name'] : get_bloginfo( 'name' );
$from_email = isset( $edd_options['from_email'] ) ? $edd_options['from_email'] : get_option( 'admin_email' );
// headers
$headers = "From: " . stripslashes_deep( html_entity_decode( $from_name, ENT_COMPAT, 'UTF-8' ) ) . " <$from_email>\r\n";
$headers .= "Reply-To: ". $from_email . "\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
// send email
wp_mail( $email_address, $subject, $message, $headers );
}
}
}
add_action( 'edd_create_payment', 'sumobi_eddmp_create_payment', 1 );
/**
* Email Template Body
*/
function sumobi_eddmp_build_email( $first_name, $username, $password ) {
// Email body
$default_email_body = '';
if ( $first_name ) {
$default_email_body .= "Dear" . ' ' . $first_name . ",\n\n";
}
else {
$default_email_body .= "Hi" . ",\n\n";
}
$default_email_body .= "Below are your login details:" . "\n\n";
$default_email_body .= "Your Username:" . ' ' . $username . "\n\n";
$default_email_body .= "Your Password:" . ' ' . $password . "\n\n";
$default_email_body .= get_bloginfo( 'name' ) . "\n\n";
$default_email_body .= site_url();
return apply_filters( 'edd_purchase_receipt', $default_email_body, null, null );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment