How to add custom placeholders to WooCommerce email subject
It’s not uncommon for WooCommerce store owners to want to add their own custom email placeholders in the subject line of the automated email that the store sends. By default, there are a limited amount of inbuilt email subject placeholders in WooCommerce.
To add custom placeholders to your WooCommerce email subjects, we have to use one of the WooCommerce filter hooks woocommerce_email_format_string
. You will need some PHP knowledge to customize the code snippet to suit your requirements. If you are struggling, feel free to contact our WordPress experts.
/**
* @snippet Add custom placeholders to WooCommerce email subject
* @author WP Authors
* @compatible WooCommerce 3.2+
* @donate https://www.buymeacoffee.com/wpauthors
* @param [string] $string
* @param [object] $email
* @return string
*/
function wpa_filter_email_format_string( $string, $email ) {
// Get WC_Order object from email
$order = $email->object;
// Add new placeholders
$new_placeholders = array(
'{_first_name}' => $order->get_billing_first_name(),
'{_order_total}' => $order->get_total(),
'{_payment_method}' => $order->get_payment_method_title(),
);
// return the string with new placeholder replacements
return str_replace( array_keys( $new_placeholders ), array_values( $new_placeholders ), $string );
}
add_filter( 'woocommerce_email_format_string' , 'wpa_filter_email_format_string', 20, 2 );
As your requirement, you can add as many as placeholders you need. You can find the full list of available methods of the WooCommerce order object here. I used “_” underscore at the beginning of the placeholder to avoid potential conflicts.
How to use the custom WooCommerce email subject placeholders
There is no magic. You can follow the same steps just like a regular placeholder.
- Navigate to WooCommerce > Settings > Emails and select the email that you need to add the placeholder.
- Find the subject field and combine your subject with your new email subject placeholders.
Ex new order notification: New order from {_first_name} via {_payment_method}