Forked from davekiss/gist:e2b5beb37b3a1a93a3dddcf43ce51ce9
Last active
January 9, 2025 07:54
-
-
Save brianhogg/b87c216dde537ecaccdc5063858d0c3f to your computer and use it in GitHub Desktop.
Display notice in EDD if email already exists and not logged in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: EDD Prompt Login On Checkout | |
Plugin URI: http://brianhogg.com/ | |
Description: Prompt login if email exists | |
Author: Dave Kiss | |
Version: 1.0.0 | |
Author URI: http://brianhogg.com | |
License: GPL2 | |
*/ | |
// From : https://gist.github.com/davekiss/e2b5beb37b3a1a93a3dddcf43ce51ce9 | |
/** | |
* Redirect the user to the appropriate page upon login. | |
*/ | |
add_filter( 'login_redirect', function( $url, $query, $user ) { | |
if ( is_wp_error( $user ) ) { | |
return $url; | |
} | |
if ( current_user_can('administrator') ) { | |
return $url; | |
} | |
if ( empty( edd_get_cart_contents() ) ) { | |
return home_url( "/account" ); | |
} | |
return edd_get_checkout_uri(); | |
}, 10, 3 ); | |
/** | |
* Check if an email exists during checkout and encourage the user to login | |
* @return {[type]} [description] | |
*/ | |
add_action('wp_ajax_nopriv_edd_check_if_customer_email_exists', function() { | |
if ( is_user_logged_in() ) { | |
return; | |
} | |
check_ajax_referer( 'edd_check_if_customer_email_exists' ); | |
if ( empty( $_REQUEST['email'] ) ) { | |
return; | |
} | |
$email = sanitize_email( $_REQUEST['email'] ); | |
if ( username_exists($email) || email_exists( $email ) ) { | |
echo "1"; | |
die; | |
} | |
echo "0"; | |
die; | |
}); | |
function edd_check_if_email_exists_during_checkout() { | |
if ( is_user_logged_in() ) { | |
return; | |
} | |
ob_start();?> | |
<script> | |
var $emailInput = jQuery('#edd-email'); | |
var emailFound = false; | |
$emailInput.blur(function(e) { | |
var value = e.target.value; | |
jQuery.ajax({ | |
url: "/wp-admin/admin-ajax.php", | |
type: "POST", | |
data: { | |
"action": "edd_check_if_customer_email_exists", | |
"_wpnonce": "<?php echo wp_create_nonce( 'edd_check_if_customer_email_exists' ); ?>", | |
"email": value | |
}, | |
success: function(exists) { | |
if (exists == "1" && emailFound == false) { | |
jQuery("#edd-purchase-button").prop("disabled", true); | |
var emailTemplate = '<div style="margin: 15px 0 0;color: #fafafa;background: #f77530;padding: 15px;border-radius: 2px;box-shadow: 0 2px 2px rgba(0, 0, 0, 0.15);"><h2 style="margin: 0;">Welcome back!</h2><p style="color: #fafafa;">Before you purchase, <a href="/account" style="color: #292d33; font-weight: bold;">please sign in to your account.</a></p></div>'; | |
$emailInput.after(emailTemplate); | |
emailFound = true; | |
} | |
}, | |
error: function(error) { | |
console.error(error); | |
} | |
}) | |
}); | |
</script> | |
<?php | |
echo ob_get_clean(); | |
} | |
add_action('edd_purchase_form_after_user_info', 'edd_check_if_email_exists_during_checkout', 11); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment