Allowing guest checkouts in WooCommerce are great for your sales conversions. It allows customers to checkout quickly without the need to create and log in to their account.

Thus, our goal for today is to provide a third option by allowing automatic user account creation for guest users, via PHP coding.
This article will show you a way of doing that. We’ll allow guest checkout but we’ll also automatically create a user profile in the background. You will also have access to the user’s custom fields and you’ll have them marked as Guests.

These are the main blocks of our custom code to work:

  • Preparation
  • Detect if the current order is from a guest user
  • Create a user account
  • Add custom fields to the user account
  • Link past orders to the new user account
  • Reading the custom fields data
  • Wrapping up

Let’s get right into it!

Preparation

In order to add this code we’ll need two other factors. First is enabling the guest checkout in WooCommerce. You can do that under WooCommerce > Settings > Checkout > Checkout Process > Enable guest checkout:

WooCommerce Guest Users Checkout

Detecting if the current order is from a guest user

The first task of our code is detecting if the current order is coming from a guest user. In order to do that, we’ll add a snippet that takes action when you are on the thank you page. In this way you’ll see this code even if the order payment is still pending.

Since there is no direct function to detect if a user is a guest, we’ll check if there is already a user registered with this email.

This is the code to do it:

<?php
/**
* Create guest users on checkout
* @param int $order_id order id
* @author wbcom designs
*/
function wc_register_guests( $order_id ) {
 $order = new WC_Order($order_id);
 $order_email = $order->billing_email;

// check if there are any users with the billing email as user or email
 $email = email_exists( $order_email ); 
 $user = username_exists( $order_email );

// if the UID is null, then it's a guest checkout
 if( $user == false && $email == false ){

/* random password with 12 chars */
 $random_password = wp_generate_password();
 $user_id = wp_create_user( $order_email, $random_password, $order_email );

//WC guest customer identification
 update_user_meta( $user_id, 'guest', 'yes' );

//user's billing data
 update_user_meta( $user_id, 'billing_address_1', $order->billing_address_1 );
 update_user_meta( $user_id, 'billing_address_2', $order->billing_address_2 );
 update_user_meta( $user_id, 'billing_city', $order->billing_city );
 update_user_meta( $user_id, 'billing_company', $order->billing_company );
 update_user_meta( $user_id, 'billing_country', $order->billing_country );
 update_user_meta( $user_id, 'billing_email', $order->billing_email );
 update_user_meta( $user_id, 'billing_first_name', $order->billing_first_name );
 update_user_meta( $user_id, 'billing_last_name', $order->billing_last_name );
 update_user_meta( $user_id, 'billing_phone', $order->billing_phone );
 update_user_meta( $user_id, 'billing_postcode', $order->billing_postcode );
 update_user_meta( $user_id, 'billing_state', $order->billing_state );

// user's shipping data
 update_user_meta( $user_id, 'shipping_address_1', $order->shipping_address_1 );
 update_user_meta( $user_id, 'shipping_address_2', $order->shipping_address_2 );
 update_user_meta( $user_id, 'shipping_city', $order->shipping_city );
 update_user_meta( $user_id, 'shipping_company', $order->shipping_company );
 update_user_meta( $user_id, 'shipping_country', $order->shipping_country );
 update_user_meta( $user_id, 'shipping_first_name', $order->shipping_first_name );
 update_user_meta( $user_id, 'shipping_last_name', $order->shipping_last_name );
 update_user_meta( $user_id, 'shipping_method', $order->shipping_method );
 update_user_meta( $user_id, 'shipping_postcode', $order->shipping_postcode );
 update_user_meta( $user_id, 'shipping_state', $order->shipping_state );

/**
 * link past orders to this newly created customer
 */
 wc_update_new_customer_past_orders( $user_id );
 }
}
add_action( 'woocommerce_thankyou', 'wc_register_guests', 10, 1 );