简体   繁体   中英

Adding custom field in WooCommerce new user registration email

I wrote a custom user registration form that handles login and registration for my WordPress Woocommerce web site.

When a user registers via my custom form handler I triggered Woocommerce to send the new user an email. But I need to add an activation code on the email.

Is it possible to do so?

Thanks

First you need to register this activation code in the user metadata.

For example using in your code something like:

// Set your activation code in the user meta
$activation_code = 'dHu12548-oh$r' // example for a generated activation code
// Saving the activation code in user meta data.
update_user_meta( $user_id, 'activation_code', $activation_code );

Then you can use a custom function hooked in woocommerce_email_header action hook:

add_action( 'woocommerce_email_header', 'custom_email_new_account', 100, 2 );
function custom_email_new_account( $email_heading, $email ) {
    if ( 'customer_new_account' === $email->id ){
        $user_id = $email->object->ID;
        $activation_code = get_user_meta( $user_id, 'activation_code', $true );
        // Displaying the activation code
        printf( __( 'Here is your activation code: %s', 'woocommerce' ), '<strong>' . esc_html( $activation_code ) . '</strong>' );
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.


Or you can insert in the WooCommerce template customer-new-account.php this similar code:

<?php
    if ( 'customer_new_account' === $email->id ){
        $user_id = $email->object->ID;
        $activation_code = get_user_meta( $user_id, 'activation_code', $true );
        // Displaying the activation code
        printf( __( 'Here is your activation code: %s', 'woocommerce' ), '<strong>' . esc_html( $activation_code ) . '</strong>' );
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM