0

I'm trying to get this to work but I can't find a plugin and I'm not a developer as such.

What I'm hoping is to be able to override the relevant Woocommerce email template file to include a snippet that identifies the customer's automatically created password from their email address, and uses it to set up a link which logs them into their account (if they're not already logged in) and takes them directly to the set new password form, or something similar.

How difficult would this be to do?

Here is the snippet I'm using for automatic account creation, in case it's relevant:

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 );

}

}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.