WooCommerce: Supercharge Your Admin Emails by Adding Custom Fields
Want to keep your finger on the pulse of your WooCommerce store? You’re probably already using admin email notifications to stay informed about new orders. But what if you need *more* information in those emails? What if you want to see specific customer details, product attributes, or custom fields right in your inbox?
That’s where adding custom fields to your WooCommerce admin email notifications comes in handy! This guide will walk you through how to do it, step-by-step, even if you’re new to coding. We’ll use simple explanations and real-world examples to make it easy.
Why Add Custom Fields to WooCommerce Admin Emails?
Think about a scenario: You sell custom-engraved pens. A customer places an order, and you need to know the exact engraving text they requested *immediately*, without logging into the WooCommerce admin panel. Adding a custom field for “Engraving Text” to the admin email would solve this problem.
Here’s why adding custom fields to your WooCommerce admin emails is beneficial:
- Improved Efficiency: Get essential order details at a glance, saving you time and effort.
- Enhanced Communication: Pass crucial information to your fulfillment team directly from the email notification. Imagine you sell t-shirts with custom designs. The admin email could contain a link to the uploaded design file.
- Reduced Errors: Access relevant data quickly, minimizing the risk of mistakes during order processing. For example, if you sell perishable goods, a “Delivery Date” custom field in the email ensures timely handling.
- Better Customer Service: Be better prepared to handle customer inquiries by having all necessary information readily available.
- A WooCommerce store: Obviously!
- A basic understanding of PHP: Don’t worry, we’ll keep it simple.
- Access to your theme’s `functions.php` file or a custom plugin: We’ll be adding code, so you’ll need a place to put it. Important: Creating a child theme is highly recommended to avoid losing changes during theme updates.
- Order Meta: Custom data added to the order using a plugin or code.
- Product Meta: Custom data associated with a specific product.
- User Meta: Custom data about the customer placing the order.
Prerequisites
Before we begin, make sure you have the following:
Step 1: Identify the Custom Field
First, you need to identify the custom field you want to display in the email. This could be:
For our examples, we’ll use Order Meta. Let’s assume you’re using a plugin that adds a custom field called `_special_instructions` to each order. This field contains any special instructions the customer might have.
Step 2: Add Code to Your `functions.php` (or Custom Plugin)
Open your theme’s `functions.php` file (or your custom plugin file) and add the following code:
<?php
/
* Add custom fields to the WooCommerce admin email notifications.
*
* @param WC_Order $order Discover insights on How To Display An External Website In Woocommerce The order object.
* @param bool $sent_to_admin Whether the email is being sent to the admin.
* @param bool $plain_text Whether the email is in plain text format.
*/
function add_custom_fields_to_admin_email( $order, $sent_to_admin, $plain_text = false ) {
// Only proceed if the email is being sent to the admin.
if ( ! $sent_to_admin ) return;
// Get the ‘Special Instructions’ value from order meta.
$special_instructions = get_post_meta( $order->get_id(), ‘_special_instructions’, true );
// Check if the field has a value. If not, don’t display it.
if ( ! empty( $special_instructions ) ) {
// Display the field.
echo ‘
Special Instructions:
‘; // Email Header
echo ‘
‘ . esc_html( $special_instructions ) . ‘
‘; // Display content
}
}
// Hook the function to the woocommerce_email_order_details hook
add_action( ‘woocommerce_email_order_details’, ‘add_custom_fields_to_admin_email’, 10, 3 );
?>
Let’s break down what this code does:
- `add_custom_fields_to_admin_email( $order, $sent_to_admin, $plain_text = false )`: This is the function that will add the custom field to the email. It takes the order object, a flag indicating if the email is for the admin, and a flag to determine if the email is plain text.
- `if ( ! $sent_to_admin ) return;`: This checks if the email is being sent to the admin. Explore this article on How To Setup A Payment Plan In Woocommerce If not, the function stops.
- `$special_instructions = get_post_meta( $order->get_id(), ‘_special_instructions’, true );`: This line retrieves the value of the custom field `_special_instructions` from the order. `get_post_meta()` is a standard WordPress function to retrieve metadata. The first argument ` $order->get_id()` retrieves the Order ID, the second argument is the key of the meta data.
- `if ( ! empty( $special_instructions ) )`: This checks if the custom field has a value. We only want to display it if it actually contains data.
- `echo ‘
Special Instructions:
‘;` and `echo ‘
‘ . esc_html( $special_instructions ) . ‘
‘;`:
These lines output the custom field’s label (“Special Instructions”) and its value in the email. `esc_html()` is used to sanitize the output, preventing potential security vulnerabilities. - `add_action( ‘woocommerce_email_order_details’, ‘add_custom_fields_to_admin_email’, 10, 3 );`: This is the most important part. It tells WordPress to run the `add_custom_fields_to_admin_email` function when WooCommerce sends the order details in the email. The arguments are:
- `’woocommerce_email_order_details’` : The hook that gets triggered when sending email order details.
- `’add_custom_fields_to_admin_email’` : The name of the function we want to run.
- `10` : The priority of the function (lower numbers run earlier).
- `3` : The number of arguments the function accepts (in this case, the `$order`, `$sent_to_admin`, and `$plain_text` variables).
Step 3: Test Your Implementation
Place a test order and include data in the `_special_instructions` field (or whichever field you’re using). Then, check the admin email notification. You should see the “Special Instructions” and its value displayed in the email.
Customizing the Output
You can customize the output of the custom field to suit your needs. Here are a few ideas:
- Change the Label: Modify the `echo ‘
Special Instructions:
‘;` line to display a different label.
- Add Formatting: Use HTML tags like ``, ``, or `
- ` to format the output. For example:
echo 'Special Instructions: ' . esc_html( $special_instructions ) . '
';
- Conditional Display: You can add conditions to display different content based on the value of the custom field. For instance:
if ( $special_instructions == 'Urgent' ) { echo 'Read more about How To Dropship Bulk Order Using Woocommerce red">URGENT! Handle this order ASAP!
'; } else { echo 'Special Instructions: ' . esc_html( $special_instructions ) . '
'; }
Displaying Multiple Custom Fields
You can easily display multiple custom fields by adding more code blocks within the `add_custom_fields_to_admin_email` function. For example:
<?php
function add_custom_fields_to_admin_email( $order, $sent_to_admin, $plain_text = false ) {
if ( ! $sent_to_admin ) return;
$special_instructions = get_post_meta( $order->get_id(), ‘_special_instructions’, true );
$delivery_date = get_post_meta( $order->get_id(), ‘_delivery_date’, true ); // Assume you have another custom field
if ( ! empty( $special_instructions ) ) {
echo ‘
Special Instructions:
‘;
echo ‘
‘ . esc_html( $special_instructions ) . ‘
‘;
}
if ( ! empty( $delivery_date ) ) {
echo ‘
Preferred Delivery Date:
‘;
echo ‘
‘ . esc_html( $delivery_date ) . ‘
‘;
}
}
add_action( ‘woocommerce_email_order_details’, ‘add_custom_fields_to_admin_email’, 10, 3 );
?>
Considerations for Plain Text Emails
The `$plain_text` parameter in the `add_custom_fields_to_admin_email` function determines whether the email is being sent in HTML or plain text format. If it’s a plain text email, you should avoid using HTML tags. Instead, use simple formatting:
<?php
function add_custom_fields_to_admin_email( $order, $sent_to_admin, $plain_text = false ) {
if ( ! $sent_to_admin ) return;
$special_instructions = get_post_meta( $order->get_id(), ‘_special_instructions’, true );
if ( ! empty( $special_instructions ) ) {
if ( $plain_text ) {
echo “Special Instructions: ” . esc_html( $special_instructions ) . “nn”;
} else {
echo ‘
Special Instructions:
‘;
echo ‘
‘ . esc_html( $special_instructions ) . ‘
‘;
}
}
}
add_action( ‘woocommerce_email_order_details’, ‘add_custom_fields_to_admin_email’, 10, 3 );
Advanced Techniques
For more advanced customizations, you can explore these techniques:
- Using different hooks: WooCommerce provides various hooks for different parts of the email. Explore the WooCommerce documentation to find the most appropriate hook for your needs.
- Creating a custom email template: You can create a completely custom email template for more control over the email’s appearance.
- Integrating with plugins: If you’re using a plugin to manage custom fields, check its documentation for specific instructions on how to display the fields in emails.
Conclusion
Adding custom fields to your WooCommerce admin email notifications is a powerful way to streamline your workflow and improve order management. By following the steps outlined in this guide, you can easily display essential order details in your inbox, saving you time and effort. Remember to test your code thoroughly and use a child theme to avoid losing your changes during theme updates. Happy customizing!