0
votes

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

1

1 Answers

1
votes

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>' );
    }
?>