/*
 Theme Name:   Woodmart Child
 Description:  Woodmart Child Theme
 Author:       XTemos
 Author URI:   http://xtemos.com
 Template:     woodmart
 Version:      1.0.0
 Text Domain:  woodmart
*/
// Hide the "Create an account?" field
add_action('wp_head', 'hide_create_account_field_completely');
function hide_create_account_field_completely() {
    if (is_checkout()) {
        echo '<style>.create-account { display: none !important; }</style>';
    }
}

// Automatically enable account creation
add_action('woocommerce_checkout_process', 'force_account_creation_backend');
function force_account_creation_backend() {
    if (!is_user_logged_in()) {
        $_POST['createaccount'] = 1; // Force WooCommerce to process account creation
    }
}

// Automatically generate username and password if account creation is forced
add_filter('woocommerce_checkout_posted_data', 'auto_generate_account_details', 10, 1);
function auto_generate_account_details($data) {
    if (!is_user_logged_in() && isset($_POST['createaccount'])) {
        // Generate a username based on the email address
        if (empty($data['account_username'])) {
            $data['account_username'] = explode('@', $data['billing_email'])[0];
        }
        // Generate a random password if none is provided
        if (empty($data['account_password'])) {
            $data['account_password'] = wp_generate_password();
        }
    }
    return $data;
}