Core972 Core972 - 9 months ago 131
PHP Question

Woocommerce - override wc_create_new_customer

It's look like impossible to override this function. I'm trying to send the created user id, mail and password to another database so I added this hook :

function ex_create_new_customer( $email, $username = '', $password = '' ) {

// add to datastory BD

/* GenSalt */

$string = str_shuffle(mt_rand());
$salt = uniqid($string ,true);

$rounds = 12;

$crypted_password = crypt($_POST['password'], '$2y$'. $rounds . '$' . $salt);

/*request sql*/
$servername = "server_address";
$username = "my_user";
$passworddb = "user_pass_for_db";
$conn = new PDO("mysql:host=$servername;dbname=db_name", $username, $passworddb);

try {
$conn->exec("INSERT INTO user(user_nom, user_prenom, user_mdp, user_mail ) VALUES('".$_POST['billing_last_name']."', '".$_POST['billing_first_name']."', '".$crypted_password."', '0', '".$_POST['email']."')");
} catch(PDOException $e) {
die( $e->getMessage() );


add_action( 'wc_create_new_customer', 'ex_create_new_customerZ', 1 , 3 );

Even changing the function name to
doesn't trigger an error.

Answer Source

For me it's not the right hook to use. wc_create_new_customer is a function not an action.

In your case, you can use the action woocommerce _created_customer which is trigger right after the wp_insert_user in the wc_create_new_customer. More details

add_action('woocommerce_created_customer', 'ex_create_ new_customer', 99, 3);

Note that wc_create_new_customer function can be override if you declare a new function with the same name (the function is between function_exists statement). In this case you'll need to add both WC code and your own code.