Admin Dashboard, Checkout, My Account

WooCommerce Automatically Create User Account Only After Payment

Pre-Requisites

Make sure you have the “Allow customers to create an account during checkout” unchecked under Dashboard > WooCommerce > Settings > Account & Privacy for this snippet to work correctly

How To Implement This Solution?

Simply copy the pre-coded solution to your active theme’s functions.php or preferably the Code Snippets Plugin.

About This Solution

This snippet will automatically create a user account for customers ONLY when they pay for their order. Make sure you have the “Allow customers to create an account during checkout” unchecked under Dashboard > WooCommerce > Settings > Account & Privacy for this snippet to work correctly. With this option selected, by default, WooCommerce will create a user account for customers even if their payment fails or is on-hold. Specifically, this snippet will check a few things. It first checks the user isn’t logged in, because if they are, then they obviously already have an account. Once the order is created and the customer reaches the thank you page, the snippet checks the user doesn’t exist by looking at their email address. Finally, it checks the order status. If the order status is set to ‘processing’ or ‘completed’, then the user account is finally created. These two order statuses are indicative or payment.

/**
 * Snippet Name:	WooCommerce Create User Account After Payment
 * Snippet Author:  ecommercehints.com
 */

add_action( 'woocommerce_thankyou', 'ecommercehints_create_user_account_after_payment', 10, 1 );

function ecommercehints_create_user_account_after_payment( $order_id ) {
    // If user is logged in, do nothing because they already have an account
    if( is_user_logged_in() ) return;

    // Get the newly created order
    $order = wc_get_order( $order_id );

    // Get the billing email address
    $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 );

	// Get the order status (see if the customer has paid)
	$order_status = $order->get_status();

    // Check if the user exists and if the order status is processing or completed (paid)
    if( $user == false && $email == false && $order->has_status( 'processing' ) || $user == false && $email == false && $order->has_status( 'completed' ) ) {
        // Check on category ( multiple categories can be entered, separated by a comma )

            // Random password with 12 chars
            $random_password = wp_generate_password();

            // Firstname
            $first_name = $order->get_billing_first_name();

            // Lastname
            $last_name = $order->get_billing_last_name();

            // Role
            $role = 'customer';

            // Create new user with email as username, newly created password and user role
            $user_id = wp_insert_user(
                array(
                    'user_email' => $order_email,
                    'user_login' => $order_email,
                    'user_pass'  => $random_password,
                    'first_name' => $first_name,
                    'last_name'  => $last_name,
                    'role'       => $role,
                )
            );

            // (Optional) 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_state', $order->billing_state );
            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 );

            // 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_state', $order->shipping_state );
            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 );

            // Link past orders to this newly created customer
            wc_update_new_customer_past_orders( $user_id );

    }
}

100 WooCommerce Conversion Rate Optimisation Tips

This field is for validation purposes and should be left unchanged.

Let’s collaborate!

Need to outsource WordPress development?

Join forces with UnlimitedWP for an outsourced white label web development service you can truly rely on.

First Timer Here?

Sign up to receive 20% off on your first month with us.

26027
WELCOME OFFER