Skip to content

Instantly share code, notes, and snippets.

@tnog
Created March 30, 2017 05:08
Show Gist options
  • Save tnog/d4429308de47d25c814cb5bb82b38c56 to your computer and use it in GitHub Desktop.
Save tnog/d4429308de47d25c814cb5bb82b38c56 to your computer and use it in GitHub Desktop.
WooCommerce: Send a custom admin email when a new customer creates a new account.
<?php
namespace Your\Custom\Namespace;
/**
* Send a custom admin email when a new customer creates a new account.
* @param integer $user_id The ID of the user whose data should be retrieved.
* @return [type] [description]
*/
function customer_registration_email_alert( $user_id ) {
$user = get_userdata( $user_id );
$first_name = null;
$last_name = null;
$role = $user->roles;
$email = $user->user_email;
// Retrieve $_POST values from WC signup form
// Note read details in the codex, but you can't rely on 'user_register' hook to retrieve first_name, last_name values.
// Not all values are stored in database when this hook runs https://codex.wordpress.org/Plugin_API/Action_Reference/user_register
if ( isset( $_POST['sr_firstname'] ) ) {
$first_name = $_POST['sr_firstname'];
}
if ( isset( $_POST['sr_lastname'] ) ) {
$last_name = $_POST['sr_lastname'];
}
$message = sprintf( __('Rejoice someone loves us! A new customer, %1$s %2$s, with the email %3$s has registered.', 'yournamespace' ), $first_name, $last_name, $email ) ;
// If new account doesn't have the 'customer' role don't do anything.
if( !in_array( 'customer', $role ) ) {
return;
}
wp_mail( [ '[email protected]','[email protected]' ], 'New Customer Registration', $message );
}
add_action( 'user_register', __NAMESPACE__ . 'customer_registration_email_alert' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment