0

In WooCommerce, from "Adding a hidden fields to checkout and process it through order" answer thread, which generates a verification number on checkout for orders,

I am trying to figure out how to turn this into a account number instead by which I mean:

  • when the customer registers, a unique number is generated
  • that number is saved to the account
  • the number can be displayed used echo later on

This is what I have tried so far (without success) =>

OPTION 1:

add_action( 'woocommerce_register_form_start', 'user_account_nr' );
function user_account_nr() {

    $billing_account_number = '';
    if ( isset( $_POST['billing_account_number'] ) ) {
        $billing_account_number = $_POST['billing_account_number'];
    }

?>


<p class="form-row form-row-first">
<input type="hidden" class="input-hidden" name="billing_account_number" id="reg_billing_account_number" value="<?php if ( ! empty( $_POST['billing_account_number'] ) ) esc_attr_e( $billing_account_number ); ?>" />
</p>

<div class="clear"></div>
<?php
}


add_action('woocommerce_created_customer', 'add_account_number');
function add_account_number($user_id)
{
    $accountnumber = wp_rand(10000,99999);
    update_user_meta($user_id, 'billing_account_number', $accountnumber);
}

OPTION 2:

add_action('user_register','aacount_on_user_register');
function account_on_user_register($user_id){
    $last_name = $_POST['last_name'];
    if ( isset($last_name) ){

       $account_id = wp_rand(10000,99999) . $user_id;
       add_user_meta( $user_id, 'account_id', $account_id);
    }
}

/* Add user "account_id" meta when user registers */
add_action('woocommerce_created_customer', 'admin_email_on_registration', 10 , 1);
function admin_email_on_registration($user_id) {
    $user = wp_get_current_user();
    $last_name =  $user->last_name; 
    if ( isset($last_name) ){

       $account_id = wp_rand(10000,99999) . $user_id;
       add_user_meta( $user_id, 'account_id', $account_id);
    }
}

Here is a third option I was trying to make work:

function uuid() {
  return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
      mt_rand(0, 0xffff), mt_rand(0, 0xffff),
      mt_rand(0, 0xffff),
      mt_rand(0, 0x0fff) | 0x4000,
      mt_rand(0, 0x3fff) | 0x8000,
      mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  );
}

add_action('woocommerce_created_customer', 'generate_unique_id_after_user_created', 10, 3);

function generate_unique_id_after_user_created( $user_id, $form_id,$sub_id ) {
  if( !empty( $user_id ) ) {
    $user_data = array(
      'ID' => $user_id,
      'user_login' => uuid(),
      'user_email' => '',
    );

    $updated_user_id = wp_update_user( $user_data );

    if ( is_wp_error( $updated_user_id ) ) {
      // There was an error, probably that user doesn't exist.
    } else {
      // Success!
    }
  }
}
  • This is not a good example to follow, for what you are trying to do, as you want to get a **unique** registration number for each customer (or required customer type). So you need to rethink that differently and better explain the context on a new question, providing some custom code of what you have related. – LoicTheAztec May 20 '19 at 10:13
  • @LoicTheAztec I understand, but this is the best example of "how" that I could find. Let me see if I can get something together. I'll notify you here. –  May 20 '19 at 10:38
  • @LoicTheAztec I have updated this question and given code for the two examples that I have tried to make work on my own without success. Thanks for looking and for helping. –  May 20 '19 at 10:43
  • @LoicTheAztec any idea on how to accomplish what I need? –  May 21 '19 at 08:04
  • @LoicTheAztec any ideas on this yet? I've tried other things as well but nothing works. –  May 22 '19 at 16:06

0 Answers0