WooCommerce的客户帐号

时间:2019-05-20 07:55:56

标签: woocommerce

在WooCommerce中,来自Adding a hidden fields to checkout and process it through order答案线程,该线程在结帐订单时生成验证码,

我试图弄清楚如何将其转换为帐号,而不是我的意思:

  • 客户注册时会生成一个唯一的号码
  • 该号码已保存到帐户中
  • 以后可以在回显中显示该数字

这是我到目前为止尝试过的(没有成功)=>

选项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);
}

选项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);
    }
}

这是我正在尝试的第三个选择:

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!
    }
  }
}

0 个答案:

没有答案