Skip to content

Instantly share code, notes, and snippets.

@NickGreen
Last active February 27, 2020 00:09
Show Gist options
  • Select an option

  • Save NickGreen/05a2d16233d39f71fde48bee46972820 to your computer and use it in GitHub Desktop.

Select an option

Save NickGreen/05a2d16233d39f71fde48bee46972820 to your computer and use it in GitHub Desktop.
Create username from email, just using the part before the "@"
<?php
add_filter( 'woocommerce_new_customer_data', 'customer_username_based_on_email', 20, 1 );
function customer_username_based_on_email( $new_customer_data ){
// get the first billing name
if(isset($_POST['billing_email'])) $billing_email = $_POST['billing_email'];
if( ! empty($billing_email) && ! in_array( $_POST['billing_email'] ) ){
// Replacing 'user_login' in the user data array, before data is inserted
$arr = explode("@", $billing_email, 2);
$first_part = $arr[0];
$new_customer_data['user_login'] = $first_part;
}
return $new_customer_data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment