Skip to content

Instantly share code, notes, and snippets.

@pavlo-bondarchuk
Created March 27, 2020 10:50
Show Gist options
  • Select an option

  • Save pavlo-bondarchuk/26b961d27e884826185c487b93fa51a2 to your computer and use it in GitHub Desktop.

Select an option

Save pavlo-bondarchuk/26b961d27e884826185c487b93fa51a2 to your computer and use it in GitHub Desktop.
woocommerce checkout - login user if email exists
add_action( 'woocommerce_before_checkout_process', 'wc_before_checkout_process', 999 ); // hook, function name, priority
function wc_before_checkout_process(){ // function name
foreach ( WC()->cart->get_cart() as $cart_item ) { // loop wc cart
$item_id = $cart_item['data']->get_id(); // item id
$is_ticket = get_post_meta( $item_id, '_tutor_product', true ); // get post meta field '_tutor_product'
if( $is_ticket ){ // if ticket
if( isset( $_POST['primarycontactemail'] ) ){ // if field exists
if ( !empty( $_POST['primarycontactemail'] ) ){ // if field not empty
if ( ! is_user_logged_in() ) { // if user not logged in
nocache_headers(); // clear headers
wp_clear_auth_cookie(); // clear cookie
$user_id = email_exists( $_POST['primarycontactemail'] ); // get user id by email from email field
wp_set_auth_cookie( $user_id ); // set auth cookie
WC()->session->set( 'reload_checkout', true ); // reload page and log-in user
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment