• Resolved Group Of Oceninfo

    (@princebhalani143)


    Hello Everyone,

    While creating manual orders in WooCommerce backend, user accounts are not being generated automatically. Is there a script or solution to ensure user creation? Please provide guidance.

    WooCommerce and WordPress Details:

    • WooCommerce Version: 8.4.0
    • WordPress Version: 6.4.2

    Thanks,
    Group of Oceninfo

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Group Of Oceninfo

    (@princebhalani143)

    Hi Everyone,

    To address the issue of user accounts not being generated automatically when creating manual orders in WooCommerce, we can use the following code snippet. This code should be added to your theme’s functions.php file or a custom plugin:

    // Check if the request is in the WordPress admin area
    if (is_admin()) {
        add_action('woocommerce_new_order', 'auto_create_user_account_for_orders', 10, 1);
    
        function auto_create_user_account_for_orders($order_id) {
            // Check if the current user has the 'manage_woocommerce' capability
            if (current_user_can('manage_woocommerce')) {
                // Get the order object
                $order = wc_get_order($order_id);
    
                // Check if the user is already registered (based on billing email)
                $user_email = $order->get_billing_email();
                $user = get_user_by('email', $user_email);
    
                // If user doesn't exist, create a new user account
                if (!$user) {
                    // Generate a username based on the billing email
                    $username = sanitize_user(current(explode('@', $user_email)), true);
    
                    // Generate a random strong password
                    $password = generate_strong_password();
    
                    // Create the new user
                    $user_id = wp_create_user($username, $password, $user_email);
    
                    // Set the user role to customer (adjust as needed)
                    $new_user = new WP_User($user_id);
                    $new_user->set_role('customer');
    
                    // Update the order with the user ID
                    $order->set_customer_id($user_id);
                    $order->save();
    				
    				$site_name = get_bloginfo('name');
    
                    // Notify the user with custom login credentials
                    //custom_wc_new_user_notification_email(array('password' => $password), $user_id, get_option('blogname'));
    				custom_wc_new_user_notification_email(array('password' => $password), $user_id, $site_name);
                }
            }
        }
    
        // Custom function to generate a strong password
        function generate_strong_password() {
            // Adjust the password length and complexity as needed
            $password = wp_generate_password(12, true);
            return $password;
        }
    
        // Custom function to override WooCommerce new user email content
        function custom_wc_new_user_notification_email($args, $user_id, $site_name) {
            // Check if the user is already registered (based on billing email)
            $user_email = get_userdata($user_id)->user_email;
    
            // Set default values for subject and message
    
    		$subject = __('Welcome to ') . $site_name;
            $message = sprintf(__('Hello %s,'), get_user_by('ID', $user_id)->user_login) . "\r\n\r\n";
            $message .= __('Thank you for creating an account on our site.') . "\r\n\r\n";
            $message .= __('You can log in to your account using the following details:') . "\r\n\r\n";
    		$message .= '<a href="' . home_url('/my-account/') . '">Click here to log in</a>' . "\r\n\r\n";
    		$message .= '<a href="https://your-login-url.com">Click here to log in</a>';
            $message .= sprintf(__('Username: %s'), get_user_by('ID', $user_id)->user_login) . "\r\n";
            $message .= sprintf(__('Password: %s'), isset($args['password']) ? $args['password'] : 'YourCustomPassword') . "\r\n\r\n"; // Replace 'YourCustomPassword' with the generated password
            $message .= __('For security reasons, we recommend that you change your password after logging in for the first time.') . "\r\n\r\n";
            $message .= __('Thank you for choosing us!') . "\r\n\r\n";
            $message .= __('Best regards,') . "\r\n";
            $message .= $site_name;
    
            // Send the modified email to the customer's registered email
            wp_mail($user_email, $subject, $message);
    
            // Return the modified email message
            return array($subject, $message);
        }
    
    }
    

    Please make sure to test this code on a staging environment before applying it to your live site. Additionally, it’s essential to have a backup of your site before making any changes.

    Thanks,
    Group of Oceninfo

    Plugin Support Zubair Zahid (woo-hc)

    (@doublezed2)

    Hello Group Of Oceninfo,

    Thank you for contacting WooCommerce support.

    It appears you’ve found a solution that works well for you and you’ve shared it with the community, thank you for contributing!
    Solutions shared by users are valuable to the community, and it’s great that others might benefit from your findings.

    If you have any more questions or need further assistance, please don’t hesitate to ask. 🙂

    Best regards.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.