I am trying to send a custom WooCommerce mail with user credentials after an order with certain product. To explain further: Someone buys a product
- I have to check if the order contains a certain product id
- I have to check if the customer is registered as a user already, if not it should create a user
- Then it should send a custom mail with those credentials to the customer
I am struggling to get the following code to work. Especially I dont know how I can add my custom email-message with the credentials and a custom login link. Do you know a solution?
add_action( 'woocommerce_thankyou', 'check_order_product_id' );
function check_order_product_id( $order_id ){
$order = wc_get_order( $order_id );
$items = $order->get_items();
$order_email = $order->billing_email;
//create custom mail
function get_custom_email_html( $order, $heading = false, $mailer ) {
$template = 'emails/my-custom-email-i-want-to-send.php';
return wc_get_template_html( $template, array(
'order' => $order,
'email_heading' => $heading,
'sent_to_admin' => false,
'plain_text' => false,
'email' => $mailer
) );
}
// load the mailer class
$mailer = WC()->mailer();
//format the email
$recipient = $order_email;
$subject = __("Deine Login-Daten für Geomap", 'theme_name');
$content = get_custom_email_html( $order, $subject, $mailer );
$headers = "Content-Type: text/htmlrn";
//send the email through wordpress
$mailer->send( $recipient, $subject, $content, $headers );
foreach ( $items as $item_id => $item ) {
$product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
if ( $product_id === XYZ ) {
//get the user email from the order and check if registered already
function email_exists( $order_email ) {
$user = get_user_by( 'email', $order_email );
if ( $user ) {
return $wp_new_user_notification_email;
}
else {
// random password with 12 chars
$random_password = wp_generate_password();
// create new user with email as username & newly created pw
$user_id = wp_create_user( $order_email, $random_password, $order_email );
return $wp_new_user_notification_email;
}
}
}
}
}
2
Answers
Hey for the question "how I can add my custom email-message with the credentials and a custom login link",
In your code, there is this line
you could add the credentials there, you may pass other arguments as the second parameter of this functionwc_get_template_html
second, the code order will not work for what you are trying to achieve
in this line, you are sending the email
And after that, there is a foreach loop for all the items in the order and creates a function looking if the user exists.
This function is not been called.
I will suggest to take out that function from the foreach loop and call it inside if your goal is to check only if the product XYZ exists
and all of it before the email
Now as a conclusion, WP_user class doesn’t give access to the password for security reasons, and they don’t send it by email, you should not either is a very bad practice.
I will suggest you create the account and let WP send the default email and then you send another email with the link to your custom page, but not with the credentials.
That way they will receive two emails one secure and one with the special conditions.
Because you mentioned low barrier to entry being very important…
I’d recommend linking to your thank you page and having the registration link there too. You can use SSO (Google/Amazon/etc.) options so that users don’t need to register directly through WordPress.
I’m assuming the product ID has some kind of activation code? You can send that, encrypted, as a parameter to the registration page so that it’s registered in their session. Once logged in, you have attached their activation code to the user account, and you’re done.
Steps:
Customer buys
If they have an account: send them the link, and they’re done.
If they don’t:
a. Send them an email with a link with the encrypted activation code.
b. They will have to register (through SSO or through WordPress directly).
c. Once registered, you attach their product to their user account, and redirect them to the page they care about.