wordpress - 使用来自 woocommerce 字段的 billing_company 作为用户名
问题描述
我有来自 woocommerce 的用户注册表单,用户可以在其中注册以创建帐户。现在生成的用户名是名字。是否可以在 woocommerce 注册时使用 billing_company 字段作为用户名而不是名字?
解决方案
您应该使用挂钩到 woocommerce_new_customer_data 过滤器挂钩的自定义函数。此代码将结合名字和姓氏作为用户名。比使用计费公司更好
add_filter( 'woocommerce_new_customer_data', 'custom_new_customer_data', 10, 1 );
function custom_new_customer_data( $cust_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) ) {
$user_name = $first_name . ' ' . $last_name;
}
// Replacing 'user_login' in the user data array, before data is inserted
if( ! empty($user_name ) ) {
$cust_customer_data['user_login'] = sanitize_user( str_replace( ' ', '_', $user_name ) );
}
return $cust_customer_data;
}