Bulk Converting Guest Checkout Users to Registered Customers in WooCommerce

In a recent Business Bloomer Club discussion, a user with over 1600 guest checkouts wanted to convert these previous guest users into registered WooCommerce customers. Since these customers had previously checked out as guests, they didn’t have accounts created, but the user needed a way to retroactively register them with the ‘customer’ role.

Solution Recommendation: A one-off PHP script could be used to loop through all guest orders, check if a customer already has an account, and register those who don’t. This would effectively create accounts for each past guest, assigning them the ‘customer’ role and ensuring that no duplicates are created for users who already have accounts.

Sample Code:

A script inspired by Business Bloomer’s anonymize users snippet can help guide this customization:

function register_guest_customers_as_users() {
    $args = array(
        'status' => 'completed', // or other statuses relevant to your store
        'return' => 'ids',
        'meta_key' => '_customer_user',
        'meta_value' => 0,
    );

    $orders = wc_get_orders($args);

    foreach ($orders as $order_id) {
        $order = wc_get_order($order_id);
        $email = $order->get_billing_email();

        if (email_exists($email) === false) {
            $username = strstr($email, '@', true);
            $password = wp_generate_password();
            $user_id = wc_create_new_customer($email, $username, $password);

            if (!is_wp_error($user_id)) {
                // Attach this order to the new customer ID
                update_post_meta($order_id, '_customer_user', $user_id);
            }
        }
    }
}

// Run the function once, then remove or comment it out to avoid re-running.
register_guest_customers_as_users();

How It Works:

  1. Fetch Orders: Finds all guest orders by filtering for orders with _customer_user set to 0.
  2. Create Account: For each guest order, it checks if an account exists for the billing email. If not, a new user account is created.
  3. Attach Order: Links the order to the newly created customer account.

Important Considerations

  • Privacy: Ensure compliance with your privacy policy before creating accounts.
  • Run Once: This script is intended as a one-time solution. Once executed, it should be removed to prevent re-running on future guest orders.

This approach streamlines the process of converting guests into registered customers, saving time and providing long-term benefits for customer management and retention.

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Reply

Your email address will not be published. Required fields are marked *