How to add custom content to WooCommerce emails

woo emails

Recently I felt the need to add some personalized content to the emails WooCommerce sends to customers, because in one of the emails I had to send a text and a link.

Let’s see, I’ll tell you in detail…

The need

It turns out that one of the services I offer is online WordPress consulting. The booking is simple, I use WooCommerce and my favorite booking plugin, which also automatically creates the appointment in Google Calendar, so far so good.

But the problem came to me because I do the consultations with Zoom, and after creating the automatic reservation, I had to schedule the appointment in Zoom, create the link, and edit the appointment in Google Calendar to send it to the customer. Too much manual work and I like automation.

Well, it turns out that talking to a friend he told me why don’t you always use the same Zoom link? And he’s right, you can, so all I needed was to customize the WooCommerce emails and add the unique link, and maybe some text.

Let’s get to work!

The solution

In the face of this challenge you could choose 2 ways to achieve it:

  1. Modify the WooCommerce email templates: The process involves copying the affected template to the same path in your active theme and then modifying the PHP to your liking.
  2. Create an action hook that adds my custom text and link in the WooCommerce email sent to the client.

Clearly, I decided on the second method, because it does not depend on the theme, and it is a faster and cleaner process.

The code I used is this:

%MINIFYHTML3b3c964bc8db92eb23beb867f48bde7c58%
// Add custom text and link to customer client before order table add_action( 'woocommerce_email_before_order_table', 'wphelp_custom_content_customer_email', 20, 4 ); function wphelp_custom_content_customer_email( $order, $sent_to_admin, $plain_text, $email ) { if ( $email->id == 'customer_completed_order' ) { echo '<h2>¡Thanks for ordering a consultation!</h2><p>To access the online consultation the day and hour accorded follow this url and connect audio and cam: https://zoom.us/j/665?pwd=</p>'; } }

How do I adapt the code to my needs?

From the example code, besides the most obvious thing, which is to customize the echo message, perhaps the most important thing is to decide which email(s) it will be added to.

To do this you can change the $email->id == 'id_del_email' ) by changing or adding to the example (customer_completed_order) the id(s) of the email(s) you want it to be attached to, that is:

if ( $email->id == 'cancelled_order' ) {} //cancelled order if ( $email->id == 'customer_processing_order' ) {} //processing order if ( $email->id == 'customer_invoice' ) {} //customer invoice if ( $email->id == 'customer_new_account' ) {} //new customer account if ( $email->id == 'customer_note' ) {} //customer note if ( $email->id == 'customer_on_hold_order' ) {} //on hold order if ( $email->id == 'customer_refunded_order' ) {} //refunded order if ( $email->id == 'customer_reset_password' ) {} //reset password if ( $email->id == 'failed_order' ) {} //failed order if ( $email->id == 'new_order' ) {} //new order

And where do you see the IDs of the emails?

So every WooCommerce email has one, and you see it when you edit any of them in the WooCommerce settings. You only have to look at the URL of the email you have opened to modify, which will be of the type: https://mydomain.com/wp-admin/admin.php?page=wc-settings&tab=email&section=wc_email_EMAIL_ID.

For example, the ID I used in the code would be taken from the URL https://mydomain.com/wp-admin/admin.php?page=wc-settings&tab=email&section=wc_email_customer_completed_order.

Where do i put the code?

You can put the code in 2 possible locations:

  1. In the functions.php file of the active theme: If it’s a child theme there’s no problem, the only problem is that you depend on the theme to make the code work.
  2. In your own customization plugin: Independent of the theme.

I think it’s clear that my favorite option is the second one, but it works in any of them. In the emails that you have incorporated their ID in the code, your personalized content will be added before the order detail table.

Read this post in Spanish: Cómo añadir contenido personalizado a los correos electrónicos de WooCommerce

Click to rate this post!
[Total: 1 Average: 5]

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top Skip to content