Skip to content

Instantly share code, notes, and snippets.

@SeanTOSCD
Created November 8, 2019 18:37
Show Gist options
  • Save SeanTOSCD/b52a73d5948edf61bc8943eec5738669 to your computer and use it in GitHub Desktop.
Save SeanTOSCD/b52a73d5948edf61bc8943eec5738669 to your computer and use it in GitHub Desktop.
EDD checkout form - move login form to checkout cart (custom)
<?php // DO NOT COPY THIS LINE
// PLEASE READ ALL COMMENTS
/**
* This is being provide as-is directly from a custom implementation I built for the checkout form on easydigitaldownloads.com.
* It's not just this part that is custom. I made various changes to the entire checkout cart, form, and behavior. So context
* matters. The code below is specific to repositioning the login form, putting it behind a toggle link, and making sure the
* form still submits via AJAX. While this will get you most of the way, you'll have to make tweaks based on your own code.
*
* This customization involves PHP, JS, and CSS. Copy the code into the appropriate files, then work from there.
*/
/**
* PHP
*
* Markup/structure changes to be placed in functions (or similar) file
*/
// Remove the login form from the purchase form
remove_action( 'edd_purchase_form_login_fields', 'edd_get_login_fields' );
// Add link to be used to toggle the display of the login form
function eddwp_checkout_login_toggle() {
$show_login_form = edd_get_option( 'show_register_form', 'none' );
if ( ! is_user_logged_in() && ( 'login' === $show_login_form || 'both' === $show_login_form ) ) {
?>
<div class="edd-show-login-wrap">
<p id="edd-show-login">
<span class="edd-checkout-login-title">Are you already a customer?</span>
<span class="edd-checkout-login-toggle"><a href="#" class="edd-checkout-show-login-form">Log into your account.</a></span>
</p>
</div>
<?php
}
}
add_action( 'edd_before_purchase_form', 'eddwp_checkout_login_toggle', 11 );
// Add login form above the purchase form
add_action( 'edd_before_purchase_form', 'edd_get_login_fields', 12 );
/**
* JS
*
* JS needed for the toggle functionality and so the login form can use AJAX
*/
// Checkout form login toggle
$('.edd-checkout-show-login-form').on('click',function(e){
e.preventDefault();
$(this).parents( ".edd-show-login-wrap" ).siblings( "#edd_login_fields" ).toggle();
$( "#edd_login_fields #edd_user_login" ).focus();
});
// Process the login form via ajax
// Adjusted from EDD core because the login form has been moved
$body.on('click', '#edd_checkout_wrap #edd_login_fields input[type=submit]', function(e) {
e.preventDefault();
var complete_purchase_val = $(this).val();
$(this).val(edd_global_vars.purchase_loading);
$(this).after('<span class="edd-loading-ajax edd-loading"></span>');
var data = {
action : 'edd_process_checkout_login',
edd_ajax : 1,
edd_user_login : $('#edd_login_fields #edd_user_login').val(),
edd_user_pass : $('#edd_login_fields #edd_user_pass').val(),
edd_login_nonce : $('#edd_login_nonce').val(),
};
$.post(edd_global_vars.ajaxurl, data, function(data) {
if ( $.trim(data) == 'success' ) {
$('.edd_errors').remove();
window.location = edd_scripts.checkout_page;
} else {
$('#edd_login_fields input[type=submit]').val(complete_purchase_val);
$('.edd-loading-ajax').remove();
$('.edd_errors').remove();
$('#edd-user-login-submit').before(data);
}
});
});
/**
* CSS
*
* Hide the login form by default so the toggle can open it
*/
// Hide the login form
#edd_login_fields {
display: none;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment