WooCommerce: Allow to Pay Without Login For Existing User
-
The problem:
We have needed to allow customers that already have a WordPress registration on a site to place an order in the WooCommerce store without having to log in first.
Forcing existing customers to log into their account is a major sales deterrent. A since lot of customers don′t remember their account details, they often have to go though a password reset first. But if we force them to do (log in to their account, but since they don′t remember their passwords they have to do a password reset first) this whilst in the middle of ordering, they will just abandon the order and go somewhere else.
The decision:
Our team have developed the solution to allow orders from existing customers without logging in (Allow Pay for Order Without Login).
The solution have been tested on WooCommerce v5.3.0 and v5.4.0 (June 2021). WordPress v5.7.2.
The system uses e-mail address to recognize whether is it new customer or old. If it new user, then new account will be create during a checkout as usual. If this is a new order from OLD user with old e-mail address in database, then new order will be added to existing WordPress profile. There won’t be any messages like “user already registered, please, login)
How does the code works?
When a non-logged in user with existing profile clicks on “proceed to checkout” button, the code use cookie to authenticate user without password for a very short time to trick the woocommerce system. Then immediately logs user out and moves on to the normal checkout process.
Here is the code. You need to put it in your theme “function.php” file (to end of the file or to top of the file):
// Allow WooCommerce existing customers to checkout without being logged in (allow orders from existing customers in WooCommerce without logging in) function your_custom_function_name( $allcaps, $caps, $args ) { if ( isset( $caps[0] ) ) { switch ( $caps[0] ) { case 'pay_for_order' : $order_id = isset( $args[2] ) ? $args[2] : null; $order = wc_get_order( $order_id ); $user = $order->get_user(); $user_id = $user->ID; if ( ! $order_id ) { $allcaps['pay_for_order'] = true; break; } $order = wc_get_order( $order_id ); if ( $order && ( $user_id == $order->get_user_id() || ! $order->get_user_id() ) ) { $allcaps['pay_for_order'] = true; } break; } } return $allcaps; } add_filter( 'user_has_cap', 'your_custom_function_name', 10, 3 ); add_filter( 'woocommerce_checkout_posted_data', 'ftm_filter_checkout_posted_data', 10, 1 ); function ftm_filter_checkout_posted_data( $data ) { $email = $data['billing_email']; if ( is_user_logged_in() ) { } else { if (email_exists( $email)){ $user = get_user_by( 'email', $email ); if ($user){ $user_id = $user->ID; wc_set_customer_auth_cookie($user_id); session_start(); $_SESSION['p33'] = "133"; $_SESSION['u'] = $user_id; } else { $user_id = false; } } } return $data; } add_action( 'woocommerce_new_order', 'clearuser' ); function clearuser($data) { if ($_SESSION['p33']==133){ //WC()->session->set('pp1',"0"); nocache_headers(); wp_clear_auth_cookie(); $yourSession= WP_Session_Tokens::get_instance($_SESSION['u']); $yourSession->destroy_all(); $_SESSION['p33']=''; $_SESSION['u']=''; } } //End Allow Woocommerce Order Pay Without LogIn
- The topic ‘WooCommerce: Allow to Pay Without Login For Existing User’ is closed to new replies.