• Resolved maycal

    (@maycal)


    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
    • This topic was modified 3 years, 5 months ago by maycal.
    • This topic was modified 3 years, 5 months ago by maycal.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter maycal

    (@maycal)

    This was not a support question, this was the solution. I wrote it to mark the topic as “resolved”.

    Thread Starter maycal

    (@maycal)

    Forgot to say, that the code also allow to pay for open order without logging in. I mean that you can manually create an order in WooCommerce admin dashboard, and then give a link to customer for pay. So, with the code located above, the customer dont need to provide login and password. The customer can pay without any “you need to log in to pay for this order…”

    So the code above do the two things:

    1. Allow pay for order without logging in during the usual order using “add to cart” (recognizing customer using e-mail that he provide at form during checkout and then add his new order to existing profile. It can be convenient for LMS LearnPress for example – non-logged old user with old e-mail, buys a new course and new course automatically adding to his account)

    2. Allow pay for open order which been created through admin dashboard without needed of entrance in account from the side of an user.

    • This reply was modified 3 years, 5 months ago by maycal.
    • This reply was modified 3 years, 5 months ago by maycal.
    Thread Starter maycal

    (@maycal)

    The code I have provided above may not work with some payment gateways (payment methods).

    If you will get “Bad gateway” error, then simply remove this line:

    wp_clear_auth_cookie();

    This line don’t need because there are to another lines:

    $yourSession= WP_Session_Tokens::get_instance($_SESSION['u']);
    $yourSession->destroy_all();

    which kill sessions that was created to trick WooCommerce.

    susannejonsson

    (@susannejonsson)

    Thanks, I hope this will solve my problem.

    Just a quick question – I think we are a lot of WooCommerce users that want to make it easier for our existing customers to buy a new course or a product.

    Is this solution possible to create in a plugin extension? That would make it so much easier for us to enable it in our shops.

    // Susanne

    Thread Starter maycal

    (@maycal)

    Please, address you message to WooCommerce developers team.

    susannejonsson

    (@susannejonsson)

    ok, I’ll do that

    Abiola Ogodo

    (@oaoyadeyi)

    I’m going to mark this as resolved – we’ll be here if and/or when you are ready to continue.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘WooCommerce: Allow to Pay Without Login For Existing User’ is closed to new replies.