Last active
December 1, 2021 06:48
-
-
Save xlplugins/08554f3accdc95dfecdb160430e965ec to your computer and use it in GitHub Desktop.
Create account in background and Login from step 1 of AeroCheckout
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 | |
add_action( 'wp_ajax_nopriv_wfacp_custom_create_account', 'wfacp_custom_create_account' ); | |
function wfacp_custom_create_account() { | |
WFACP_AJAX_Controller::check_nonce(); | |
$resp = [ | |
'msg' => '', | |
'status' => false, | |
]; | |
if ( isset( $_POST['data'] ) ) { | |
$post = $_POST['data']; | |
$wfacp_id = absint( $post['wfacp_id'] ); | |
if ( isset( $post['wfacp_billing_email'] ) && '' !== $post['wfacp_billing_email'] && isset( $post['wfacp_account_password'] ) && '' !== $post['wfacp_account_password'] ) { | |
$wfacp_billing_email = trim( $post['wfacp_billing_email'] ); | |
$newpassword = trim( $post['wfacp_account_password'] ); | |
// Check that user doesn't already exist | |
if ( ! username_exists( $wfacp_billing_email ) && ! email_exists( $wfacp_billing_email ) ) { | |
// Create user and set role to customer | |
remove_all_actions( 'user_register' ); | |
$user_id = wp_create_user( $wfacp_billing_email, $newpassword, $wfacp_billing_email ); | |
if ( is_int( $user_id ) ) { | |
$wp_user_object = new WP_User( $user_id ); | |
$wp_user_object->set_role( 'customer' ); | |
wp_set_auth_cookie( $user_id, true ); | |
update_user_meta( $user_id, '_wfacp_post_created', 'yes' ); | |
$resp['status'] = true; | |
WC()->cart->set_session(); | |
} | |
} | |
} | |
} | |
WFACP_AJAX_Controller::send_resp( $resp ); | |
} | |
add_action( 'wp_footer', function () { | |
if ( is_user_logged_in() ) { | |
$meta = get_user_meta( get_current_user_id(), '_wfacp_post_created', true ); | |
if ( $meta == 'yes' ) { | |
delete_user_meta( get_current_user_id(), '_wfacp_post_created' ); | |
?> | |
<script> | |
document.addEventListener('DOMContentLoaded', function () { | |
(function ($) { | |
'use strict'; | |
$('.wfacp_next_page_button').eq(0).trigger('click'); | |
})(jQuery); | |
}); | |
</script> | |
<?php | |
} | |
return; | |
} | |
?> | |
<script> | |
(function ($) { | |
'use strict'; | |
let account_created = false; | |
$(document.body).on('click', '.wfacp_next_page_button', function (e, obj) { | |
if (true == account_created) { | |
return; | |
} | |
if ($(this).data('current-step') === 'single_step') { | |
let billing_email = $("#billing_email").val(); | |
let account_password = $("#account_password").val(); | |
let prepare_data = { | |
'wfacp_billing_email': billing_email, | |
'wfacp_account_password': account_password | |
}; | |
let url = wfacp_frontend.admin_ajax; | |
$('#wfacp_checkout_form .wfacp_page').hide(); | |
$('#wfacp_checkout_form .single_step').show().block({ | |
message: null, | |
overlayCSS: { | |
background: '#fff', | |
opacity: 0.6 | |
} | |
}); | |
setTimeout(function () { | |
$.ajax({ | |
'url': url, | |
'type': 'POST', | |
async: false, | |
'data': { | |
'action': 'wfacp_custom_create_account', | |
'wfacp_nonce': wfacp_frontend.wfacp_nonce, | |
'data': prepare_data | |
}, | |
success: function (rsp) { | |
if (true == rsp.status) { | |
account_created = true; | |
$("#account_password").remove(); | |
window.location.reload(0); | |
} else { | |
$('#wfacp_checkout_form .single_step').unblock(); | |
} | |
} | |
}); | |
}, 500); | |
} | |
}); | |
})(jQuery); | |
</script> | |
<?php | |
}, 1005 ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment