在WooCommerce注册中通过结算完整名称自动生成的用户名

时间:2017-08-04 23:04:16

标签: php wordpress woocommerce registration username

所以我已经在我的WooCommerce网上商店的注册页面添加了各种自定义字段,如下所示:

/* ---------------------- Registration page ----------------------- */
//Add extra fields in registration form
add_action('woocommerce_register_form_start','my_extra_register_fields');
function my_extra_register_fields(){?>
    <p class="woocommerce-FormRow form-row form-row-first">
        <label for="reg_billing_first_name"><?php _e('First Name','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if(! empty($_POST['billing_first_name'])) esc_attr_e($_POST['billing_first_name']); ?>"/>
    </p>
    <p class="woocommerce-FormRow form-row form-row-last">
        <label for="reg_billing_last_name"><?php _e('Last Name','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if(! empty($_POST['billing_last_name'])) esc_attr_e($_POST['billing_last_name']); ?>"/>
    </p>
    <div class="clearfix"></div>
    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
        <label for="reg_billing_company"><?php _e('Company Name','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_company" id="reg_billing_company" value="<?php if(! empty($_POST['billing_company'])) esc_attr_e($_POST['billing_company']); ?>"/>
    </p>
    <div class="clearfix"></div>
    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
        <label for="reg_billing_vat"><?php _e('VAT Number','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_vat" id="reg_billing_vat" value="<?php if(! empty($_POST['billing_vat'])) esc_attr_e($_POST['billing_vat']); ?>" maxlength="15" placeholder="Enter VAT Number"/>
    </p>
    <div class="clearfix"></div>
<?php
    wp_enqueue_script('wc-country-select');
    woocommerce_form_field('billing_country',array(
        'type'          => 'country',
        'class'         => array('chzn-drop'),
        'label'         => __('Country'),
        'placeholder'   => __('Choose your country.'),
        'required'      => true,
        'clear'         => true,
        'default'       => 'BE'
    ));
?>
    <p class="woocommerce-FormRow form-row form-row-first">
        <label for="reg_billing_postcode"><?php _e('Postcode / ZIP','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_postcode" id="reg_billing_postcode" value="<?php if(! empty($_POST['billing_postcode'])) esc_attr_e($_POST['billing_postcode']); ?>"/>
    </p>
    <p class="woocommerce-FormRow form-row form-row-last">
        <label for="reg_billing_city"><?php _e('Town / City','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_city" id="reg_billing_city" value="<?php if(! empty($_POST['billing_city'])) esc_attr_e($_POST['billing_city']); ?>"/>
    </p>
    <div class="clearfix"></div>
    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
        <label for="reg_billing_address_1"><?php _e('Address','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_address_1" id="reg_billing_address_1" value="<?php if(! empty($_POST['billing_address_1'])) esc_attr_e($_POST['billing_address_1']); ?>" placeholder="Street address"/>
    </p>
    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
        <input type="text" class="input-text" name="billing_address_2" id="reg_billing_address_2" value="<?php if(! empty($_POST['billing_address_2'])) esc_attr_e($_POST['billing_address_2']); ?>" placeholder="Apartment,suite,unit etc. (optional)"/>
    </p>
    <div class="clearfix"></div>
    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
        <label for="reg_billing_phone"><?php _e('Phone','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php if(! empty($_POST['billing_phone'])) esc_attr_e($_POST['billing_phone']); ?>"/>
    </p>
    <div class="clearfix"></div>
<?php
}
//Registration form fields Validation
add_action('woocommerce_register_post','my_validate_extra_register_fields',10,3);
function my_validate_extra_register_fields($username,$email,$validation_errors){
    if(isset($_POST['billing_first_name']) && empty($_POST['billing_first_name'])){$validation_errors->add('billing_first_name_error',__('A first name is required!','woocommerce'));}
    if(isset($_POST['billing_last_name']) && empty($_POST['billing_last_name'])){$validation_errors->add('billing_last_name_error',__('A last name is required!','woocommerce'));}
    if(isset($_POST['billing_company']) && empty($_POST['billing_company'])){$validation_errors->add('billing_company_error',__('A Company name is required!','woocommerce'));}
    if(isset($_POST['billing_vat']) && empty($_POST['billing_vat'])){$validation_errors->add('billing_vat_error',__('VAT number is required!','woocommerce'));}
    if(isset($_POST['billing_country']) && empty($_POST['billing_country'])){$validation_errors->add('billing_country_error',__('A country is required!','woocommerce'));}
    if(isset($_POST['billing_city']) && empty($_POST['billing_city'])){$validation_errors->add('billing_city_error',__('A city is required!','woocommerce'));}
    if(isset($_POST['billing_postcode']) && empty($_POST['billing_postcode'])){$validation_errors->add('billing_postcode_error',__('A postcode is required!','woocommerce'));}
    if(isset($_POST['billing_state']) && empty($_POST['billing_state'])){$validation_errors->add('billing_state_error',__('A state is required!','woocommerce'));}
    if(isset($_POST['billing_address_1']) && empty($_POST['billing_address_1'])){$validation_errors->add('billing_address_1_error',__('An address is required!','woocommerce'));}
    if(isset($_POST['billing_phone']) && empty($_POST['billing_phone'])){$validation_errors->add('billing_phone_error',__('A phone number is required!','woocommerce'));}
    return $validation_errors;
}
//Save extra fields when new user registers
add_action('woocommerce_created_customer','my_save_extra_register_fields'); 
function my_save_extra_register_fields($customer_id){
    if(isset($_POST['billing_first_name'])){update_user_meta($customer_id,'first_name',sanitize_text_field($_POST['billing_first_name']));update_user_meta($customer_id,'billing_first_name',sanitize_text_field($_POST['billing_first_name']));}
    if(isset($_POST['billing_last_name'])){update_user_meta($customer_id,'last_name',sanitize_text_field($_POST['billing_last_name']));update_user_meta($customer_id,'billing_last_name',sanitize_text_field($_POST['billing_last_name']));}
    if(isset($_POST['billing_company'])){update_user_meta($customer_id,'billing_company',sanitize_text_field($_POST['billing_company']));}
    if(isset($_POST['billing_vat'])){update_user_meta($customer_id,'billing_vat',sanitize_text_field($_POST['billing_vat']));}
    if(isset($_POST['billing_country'])){update_user_meta($customer_id,'billing_country',sanitize_text_field($_POST['billing_country']));}
    if(isset($_POST['billing_city'])){update_user_meta($customer_id,'billing_city',sanitize_text_field($_POST['billing_city']));}
    if(isset($_POST['billing_postcode'])){update_user_meta($customer_id,'billing_postcode',sanitize_text_field($_POST['billing_postcode']));}
    if(isset($_POST['billing_state'])){update_user_meta($customer_id,'billing_state',sanitize_text_field($_POST['billing_state']));}
    if(isset($_POST['billing_address_1'])){update_user_meta($customer_id,'billing_address_1',sanitize_text_field($_POST['billing_address_1']));}
    if(isset($_POST['billing_phone'])){update_user_meta($customer_id,'billing_phone',sanitize_text_field($_POST['billing_phone']));}
    if(isset($_POST['email'])){update_user_meta($customer_id,'billing_email',sanitize_text_field($_POST['email']));}
}

一切正常,但目前用户名为&#34;是由电子邮件地址生成的,起初并不是真正的问题,但由于我们的重点客户是企业(因为它是b2b商店),会有很多客户拥有信息@ theirwebsite.com

这导致他们的用户名是信息...这真的不是那么好。所以我的问题是,如何配置WooCommerce / WordPress以通过自定义字段生成用户名:名字(billing_first_name)和姓氏(billing_last_name)?

我喜欢这样,以便用户名只是客户的名字,比如FirstName-LastName而不是info1 ... info2 ......等......

提前感谢您提供更多信息!

1 个答案:

答案 0 :(得分:1)

您应该使用隐藏在 woocommerce_new_customer_data 过滤器挂钩中的自定义函数。以下代码将使用完整的帐单名称替换错误且重复的用户名(您将在数组中设置)

add_filter( 'woocommerce_new_customer_data', 'custom_new_customer_data', 10, 1 );
function custom_new_customer_data( $new_customer_data ){

    // Complete HERE in this array the wrong usernames you want to replace (coma separated strings)
    $wrong_user_names = array( 'info', 'contact' );

    // get the first and last billing names
    if(isset($_POST['billing_first_name'])) $first_name = $_POST['billing_first_name'];
    if(isset($_POST['billing_last_name'])) $last_name = $_POST['billing_last_name'];

    // the customer billing complete name
    if( ! empty($first_name) || ! empty($last_name) )
        $complete_name = $first_name . ' ' . $last_name;

    // Replacing 'user_login' in the user data array, before data is inserted
    if( ! empty($complete_name) && ! in_array( complete_name, $wrong_user_names ) )
        $new_customer_data['user_login'] = sanitize_user( str_replace( ' ', '-', $complete_name ) );

    return $new_customer_data;
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

我还没有测试过这段代码,但它应该可行。

这是一个更简单的版本,它将用结算完整名称替换所有用户名。

add_filter( 'woocommerce_new_customer_data', 'custom_new_customer_data', 10, 1 );
function custom_new_customer_data( $new_customer_data ){

    // get the first and last billing names
    if(isset($_POST['billing_first_name'])) $first_name = $_POST['billing_first_name'];
    if(isset($_POST['billing_last_name'])) $last_name = $_POST['billing_last_name'];

    // the customer billing complete name
    if( ! empty($first_name) || ! empty($last_name) )
        $complete_name = $first_name . ' ' . $last_name;

    // Replacing 'user_login' in the user data array, before data is inserted
    if( ! empty($complete_name) )
        $new_customer_data['user_login'] = sanitize_user( str_replace( ' ', '-', $complete_name ) );

    return $new_customer_data;
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

我还没有测试过这段代码,但它应该可行。