woocommerce email hooks

WooCommerce Email Hooks Tutorial to Easily Customize Woo Order Emails

Last updated on

by


In this post, we’ll explain you how to use WooCommerce email hooks so you can customize your WooCommerce order emails without using a plugin. 

So, what do WooCommerce email hooks help with? 

WooCommerce email hooks allow you to edit WooCommerce notification emails. 

But why would you want to do that? Let’s consider that for a moment before we go into the guide about WooCommerce email hooks. 

By default, WooCommerce gives you very limited options for editing the order emails it sends out. 

You get a settings page for adding a custom logo to WooCommerce emails and some limited color options. But you can’t do much with the default WooCommerce email settings. 

For example, if you want to add a recommended products section in WooCommerce order emails to try to upsell your customers or add a promotional banner, the settings page won’t let you do that. 

For that matter, using a WooCommerce email customizer plugin with a drag-and-drop builder is recommended. 

These WooCommerce email customizer plugins give you complete control not just to style your WooCommerce emails but also to add dynamic content blocks that can help increase customer engagement and create a better brand experience. 

However, sometimes you want to add or change a couple of things to your WooCommerce order emails, and using a plugin with all the extra features looks unnecessary. Especially when you just want to edit one WooCommerce email template, then it might make sense to use WooCommerce email hooks to do that instead of buying a plugin. 

If you have the technical expertise to edit theme files with confidence, you can easily use these WooCommerce email hooks to do simple customizations without any plugins. 

Let’s dive into this guide about WooCommerce email hooks. 

What are WooCommerce Email Hooks?

Hooks in WordPress and WooCommerce are essential tools that allow developers and site owners to customize and extend the functionality of their sites without directly modifying core files. 

Essentially, there are two types of hooks: action hooks and filter hooks. Action hooks let you execute custom code at specific points in the WordPress or WooCommerce execution process, such as after a post is published or when an order is placed. 

Filter hooks, on the other hand, allow you to modify data—like adjusting the content of a post before it’s displayed or altering email text before it’s sent. 

This system is designed to keep customizations separate from the core plugin or theme code, ensuring that updates do not overwrite custom work.

The way hooks work is relatively straightforward: WordPress and WooCommerce code includes specific points (hook points) where additional code can be inserted or existing data can be modified. 

Developers use the do_action() function to define action hooks and apply_filters() for filter hooks within the core code. 

To utilize these hooks, you write custom functions and then attach them to these hooks using add_action() or add_filter(). 

When the execution reaches a hook point, it checks for any custom functions attached and executes them accordingly, allowing for a high degree of customization and extension while maintaining the integrity and updateability of the core system.

Now, that we have explained what are hooks in the WordPress architecture, let’s dive into WooCommerce email hooks. 

WooCommerce email hooks are specific points in the WooCommerce code that allow developers to customize the emails sent by WooCommerce. 

  • Action hooks in the context of WooCommerce emails allow you to insert additional content or perform actions at specific points in the email processing flow. For example, you can add custom text or images to the footer of all WooCommerce emails, or you might insert a special offer right after the order details in an order confirmation email.
  • Filter hooks, on the other hand, enable you to modify the content of the emails before they are sent out. This could involve changing the subject line of an email, modifying the header or footer text, or altering the style of the email templates to better match your brand.

WooCommerce provides a variety of these hooks specifically for emails to ensure that developers can easily customize email content, structure, and styling without needing to edit core files directly.

This approach ensures that customizations are not lost during updates. By using these hooks, you can enhance the communication between your store and your customers, making it more personal and branded.

How to Use WooCommerce Email Hooks

Using WooCommerce email hooks involves writing custom functions in your theme’s `functions.php` file or in a custom plugin. 

These functions are then attached to specific hooks provided by WooCommerce to customize various aspects of the emails it sends. 

Below is a step-by-step guide on how to use these hooks effectively.

First, determine what aspect of the WooCommerce emails you want to customize. 

Do you want to add content at the end of an email, change the subject line, or alter the styling? 

Once you’ve identified your goal, find the appropriate hook. WooCommerce documentation and code reference are great places to start.

Action Hooks Example

Suppose you want to add a custom message at the end of all WooCommerce emails:

  1. Find the appropriate action hook. For adding content at the end of emails, `woocommerce_email_footer` is a suitable hook.
  1. Write your custom function. This function will output the custom message.
function my_custom_email_message() {

       echo '<p>Thanks for shopping with us! Follow us on social media.</p>';

   }  
  1. Attach your function to the hook. Use `add_action` to attach your custom function to the `woocommerce_email_footer` hook.
add_action('woocommerce_email_footer', 'my_custom_email_message');

Filter Hooks Example

If you want to change the subject line of the “New Order” email:

  1. Find the appropriate filter hook. For changing email subject lines, `woocommerce_email_subject_new_order` is a suitable hook.
  1. Write your custom function. This function will modify the subject line. It needs to accept the original subject and order details as parameters and return the modified subject.
function change_new_order_subject($subject, $order) {
return 'New order received - Order #' . $order->get_order_number();

   }   
  1. Attach your function to the hook. Use `add_filter` to attach your custom function to the `woocommerce_email_subject_new_order` hook.
add_filter('woocommerce_email_subject_new_order', 'change_new_order_subject', 10, 2);

After adding your custom code, it’s crucial to test the changes to ensure they work as expected. You can do this by triggering the specific emails you’ve customized in WooCommerce. 

For example, place an order to test the “New Order” email subject line or complete an order to see the custom message at the email’s footer.

Important Notes for Using WooCommerce Email Hooks

  • Use Child Themes: If you’re adding your custom code to a theme’s `functions.php` file, use a child theme to prevent losing your changes when the theme updates.
  • Naming Your Functions: Give your custom functions unique names to avoid conflicts with other plugins or themes. 
  • Priority Parameter: When attaching your function to a hook, you can specify the priority if you want to control the order in which hooked functions are executed. Lower numbers run first.
  • Parameter Handling: Pay attention to the number of parameters your custom function should accept, especially with filter hooks, as this can vary between hooks.

By following these steps and tips, you can effectively use WooCommerce email hooks to customize the emails your store sends, enhancing the shopping experience for your customers.

Comprehensive List of WooCommerce Email Hooks to Use

WooCommerce offers a range of email hooks that can be used to customize various aspects of the emails sent by the platform. 

Here are some of the most common and useful WooCommerce email hooks that developers and store owners can use to enhance their email communications:

Action Hooks

woocommerce_email_order_items_args – This hook allows you to modify the arguments passed to the function that outputs the order items in emails. It can be used to change how order items are displayed in email templates, such as including or excluding certain information.

woocommerce_email_order_items_table – While not directly an action or filter hook, this is part of the email templates that generate the order items table. Customizing it might involve overriding the template file rather than using a hook.

woocommerce_email_after_order_table – Triggered after the order table in emails, this hook is perfect for adding additional content or information relevant to the order, such as promotional messages or special instructions.

woocommerce_email_before_order_table – Executed before the order table in emails, it’s ideal for introducing the order summary with custom text or important announcements.

woocommerce_email_customer_details – Used within various email templates to add customer details, this hook can be utilized to customize or extend the information about the customer presented in emails.

woocommerce_email_downloads_column_$COLUMN_ID – A dynamic hook that allows adding content to specific columns in the downloadable products table within emails. It’s useful for customizing the presentation of downloadable items.

woocommerce_email_footer – Fired after the email footer template is rendered, this action hook is a great spot to add additional text, links, or branding elements to the footer of WooCommerce emails.

woocommerce_email_footer_text – Located in the email-footer.php template, this is actually a part of the template that can be customized by overriding the template file.

woocommerce_email_header – This action is triggered after the email header template is rendered, providing an opportunity to insert custom content or branding right at the top of the email.

woocommerce_email_order_details – A versatile hook that appears in various email templates, allowing you to add custom content or instructions immediately following the order details section.

woocommerce_email_order_item_quantity – Used in the email-order-items.php template, it allows for customization around the quantity display of order items in emails.

Filter Hooks

woocommerce_email_order_meta – This hook allows for the customization of the order meta data displayed in emails. It’s useful for adding or modifying additional order information, such as custom fields or specific order details, before they are included in email templates.

woocommerce_order_email_verification_grace_period – Adjusts the grace period for email verification, if you have a custom verification system in place. It’s beneficial for extending or shortening the time users have to verify their email addresses in scenarios that require email verification.

woocommerce_order_email_verification_required – Determines whether email verification is required for an order. This hook can be used to conditionally require email verification based on order details or customer information.

woocommerce_allow_restoring_email_locale – Controls whether the email locale (language and regional settings) should revert to the site’s default after sending an email. It’s useful for ensuring subsequent emails or processes use the correct locale, especially in multilingual stores.

woocommerce_allow_send_queued_transactional_email – This hook decides if a queued transactional email should actually be sent. It’s handy for adding logic to prevent certain emails from being sent under specific conditions, enhancing control over email communications.

woocommerce_allow_switching_email_locale – Allows for controlling whether WooCommerce should switch the email locale based on the order’s language. This is particularly useful in multilingual stores where emails need to match the customer’s language preference.

woocommerce_copy_email_template, woocommerce_defer_transactional_emails, woocommerce_delete_email_template – These hooks are related to managing custom email templates, including copying, deferring, or deleting them. They offer advanced customization options for managing how and when emails are sent and allowing for the use of customized email templates.

woocommerce_email – Provides a broad hook for modifying the email object itself. This could be used for advanced customizations that require access to the email properties and methods directly.

woocommerce_email_actions – Allows for adding or modifying the list of actions that trigger emails. This can extend the functionality of WooCommerce emails by triggering emails based on custom actions or events.

woocommerce_email_additional_content_$THIS->ID – A dynamic hook that lets you add additional content to specific emails based on the email ID. It’s perfect for customizing the content of various email types on a case-by-case basis.

woocommerce_email_attachments – Customize the attachments for WooCommerce emails. This hook can be used to attach files to emails, such as additional terms, conditions, or promotional materials.

woocommerce_email_classes – Modify or add custom email classes. This allows for extending the range of emails WooCommerce can send by adding custom email handlers for specific events or conditions.

woocommerce_email_content_type – Change the content type of emails from plain text to HTML or vice versa. This is crucial for customizing the format and styling of the emails sent by WooCommerce.

woocommerce_email_customer_details_fields – Customize the fields displayed in the customer details section of emails. It can be used to add, remove, or modify the information about the customer that’s included in emails.

woocommerce_email_description – Allows modifying the description of email types within the WooCommerce settings. This can help clarify the purpose of custom emails or modify the descriptions of existing emails for clarity.

woocommerce_email_downloads_columns – Adjust the columns displayed in the downloads section of emails, useful for customizing the information presented about downloadable products.

woocommerce_email_enabled_$THIS->ID, woocommerce_email_format_string, woocommerce_email_format_string_find, woocommerce_email_format_string_replace – These hooks allow for deep customization of email enablement status, formatting strings used in emails, and the ability to find and replace parts of the email content dynamically.

Hopefully, by now, you’ll have a deep understanding of WooCommerce email hooks, their use cases, and how to use them. 

If you want to use a plugin instead, check out Email Customizer for WooCommerce plugin on Codecanyon.