• Resolved calimellow

    (@calimellow)


    Hey.
    I have an issue with woocommerce. I am trying to make automatically make an user after checkout and then e-mail the username and password to the buyer.
    However this just send me a blank generated password, like this:

    View post on imgur.com

    I guess this code is faulty?
    $wc->customer_new_account($user_id, $random_password, $password_generated = true );

    
      // get all the order data
      $order = new WC_Order($order_id);
      
      //get the user email from the order
      $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 );
      
      // if the UID is null, then it's a guest checkout
      if( $user == false && $email == false ){
        
        // random password with 12 chars
        $random_password = wp_generate_password();
        
        // create new user with email as username & newly created pw
        $user_id = wp_create_user( $order_email, $random_password, $order_email );
        
        //WC guest customer identification
        update_user_meta( $user_id, 'guest', 'yes' );
        
        //send PW
        $wc = new WC_Emails();
        $wc->customer_new_account($user_id, $random_password, $password_generated = true );
        
        //first name
        update_user_meta( $user_id, 'first_name', $order->$order->get_billing_first_name() );
        update_user_meta( $user_id, 'last_name', $order->get_billing_last_name() );
        
        //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_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 );
        update_user_meta( $user_id, 'billing_state', $order->billing_state );
     
        // 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_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 );
        update_user_meta( $user_id, 'shipping_state', $order->shipping_state );
        
        // link past orders to this newly created customer
        wc_update_new_customer_past_orders( $user_id );
      }
      
    }
Viewing 1 replies (of 1 total)
  • Zach W

    (@dynamiczach)

    Automattic Happiness Engineer

    Howdy!

    I am trying to make automatically make an user after checkout and then e-mail the username and password to the buyer.

    If this is what you’re trying to do, then going to WooCommerce > Settings > Accounts & Privacy and setting this should help:


    Link to image: https://d.pr/i/FEecvG

    Have you tried that already? That will only force logged in users or users who choose to create an account at checkout to be able to check out (no guest purchases). You shouldn’t need to implement any code to send a password to the user.

Viewing 1 replies (of 1 total)
  • The topic ‘Woocommerce not sending the auto generated password.’ is closed to new replies.