Я пытался заменить имя пользователя на имя пользователя с помощью кода ниже изменено из этой ветки ответов, но получайте 500 ошибок.
Если я использую имя и фамилию, это работает, но я бы предпочел использовать только имя.
Код выглядит следующим образом:
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 billing name
if(isset($_POST['billing_first_name'])) $first_name = $_POST['billing_first_name'];
if( ( ! empty($first_name) ) ) && in_array( $new_customer_data['user_login'], $wrong_user_names ) ){
// the customer billing complete name
$first_name = $first_name;
// Replacing 'user_login' in the user data array, before data is inserted
$new_customer_data['user_login'] = sanitize_user( str_replace( $first_name ) );
}
return $new_customer_data;
}
Мой вопрос заключается в том, как мне настроить WooCommerce для генерации имени пользователя по настраиваемым полям: Имя (billing_first_name) вместо полного имени или имени пользователя?
Попробуйте следующее, чтобы заменить имя пользователя на имя для выставления счета во время оформления заказа:
add_filter( 'woocommerce_new_customer_data', 'customer_username_based_on_firstname', 20, 1 );
function customer_username_based_on_firstname( $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 billing name
if(isset($_POST['billing_first_name'])) $first_name = $_POST['billing_first_name'];
if( ! empty($first_name) && ! in_array( $_POST['billing_first_name'], $wrong_user_names ) ){
// Replacing 'user_login' in the user data array, before data is inserted
$new_customer_data['user_login'] = sanitize_user( $first_name );
}
return $new_customer_data;
}
Код помещается в файл function.php вашей активной дочерней темы (или активной темы). Проверено и работает.
Ваша ошибка исходила от
str_replace( $first_name )
, Эта функция PHP требует 3 аргумента.
Других решений пока нет …